2015-01-01 2 views
1

У меня есть приложение формы в C#. Мне нужно принимать значения radobutton в textbox.i знаю, что методы закрыты, я изменяю публикацию, но он не работал. Мне нужно принимать значения1, value2, value3 и value4 при нажатии кнопки. Может кто-нибудь сказать мне способ для получения значения полей ввода ....Получение значения от radobutton для текстового поля в C#

private void groupBox1_Enter(object sender, EventArgs e) 
    { 
     double value1; 
     if (radioButton1.Checked) 
      value1 = 0.9; 
     else if (radioButton2.Checked) 
      value1 = 0.8; 
     else if (radioButton3.Checked) 
      value1 = 0.7; 
     else if (radioButton4.Checked) 
      value1 = 0.3; 
     else if (radioButton5.Checked) 
      value1 = 0.5; 
     else 
      MessageBox.Show("Oda Tipi girilmedi."); 
    } 

    private void groupBox2_Enter(object sender, EventArgs e) 
    { 
     double value2; 
     if (radioButton6.Checked) 
      value2 = 1; 
     else if (radioButton7.Checked) 
      value2 = 0.8; 
     else if (radioButton8.Checked) 
      value2 = 0.6; 
     else 
      MessageBox.Show("İzolasyon Tipi girilmedi."); 
    } 

    private void groupBox3_Enter(object sender, EventArgs e) 
    { 
     double value3; 
     if (radioButton9.Checked) 
      value3 = 0.9; 
     else if (radioButton10.Checked) 
      value3 = 1; 
     else 
      MessageBox.Show("Cam Tipi girilmedi."); 

    } 

    private void groupBox4_Enter(object sender, EventArgs e) 
    { 
     double value4; 
     if (radioButton11.Checked) 
      value4 = 1; 
     else if (radioButton12.Checked) 
      value4 = 0.9; 
     else 
      MessageBox.Show("Formül katsayısı girilmedi."); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text))); 

    } 

ответ

0

Проблема заключается в том, что ваши ценности создается из RadioButtons локальные переменные для обработчиков методов. Вы должны удалить свои groupBox_Enter обработчики и просто обрабатывать button1_Click событие так:

private void button1_Click(object sender, EventArgs e) 
{ 
    double value1; 
    if (radioButton1.Checked) 
     value1 = 0.9; 
    else if (radioButton2.Checked) 
     value1 = 0.8; 
    else if (radioButton3.Checked) 
     value1 = 0.7; 
    else if (radioButton4.Checked) 
     value1 = 0.3; 
    else if (radioButton5.Checked) 
     value1 = 0.5; 
    else 
    { 
     MessageBox.Show("Oda Tipi girilmedi."); 
     return; //not sure if this is what you want here? 
    } 

    double value2; 
    if (radioButton6.Checked) 
     value2 = 1; 
    else if (radioButton7.Checked) 
     value2 = 0.8; 
    else if (radioButton8.Checked) 
     value2 = 0.6; 
    else 
    { 
     MessageBox.Show("Izolasyon Tipi girilmedi."); 
     return; //not sure if this is what you want here? 
    } 

    double value3; 
    if (radioButton9.Checked) 
     value3 = 0.9; 
    else if (radioButton10.Checked) 
     value3 = 1; 
    else 
    { 
     MessageBox.Show("Cam Tipi girilmedi."); 
     return; //not sure if this is what you want here? 
    } 


    double value4; 
    if (radioButton11.Checked) 
     value4 = 1; 
    else if (radioButton12.Checked) 
     value4 = 0.9; 
    else 
    { 
     MessageBox.Show("Formül katsayisi girilmedi."); 
     return; //not sure if this is what you want here? 
    } 

    textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text))); 

} 
+0

ее не работает, мне нужно получить значения из радиокнопки в случае ButtonClick .... – orcanyedal

+0

его работы теперь .. я не понимал Button1_Click метода. ти для воспроизведения .. – orcanyedal

+0

рад это слышать! подумайте об этом как ответе, если это исправлено. таким образом, другие могут выиграть в будущем –

1

Вы можете либо переместить переменные (value1 и т.д.) в рамках класса или поместить все в обработчик события button1_Click как @SteveDanner имеет или вы можете написать более общее решение. Его можно легко увеличить, если вы создадите дополнительные параметры (RadioButtons).

// Store values for each RadioButton in a dictionary. 
private Dictionary<RadioButton, double> values = 
    new Dictionary<RadioButton, double>(); 

private Dictionary<GroupBox, string> messages = 
    new Dictionary<GroupBox, string>(); 

public Form1() 
{ 
     InitializeComponent(); 

     // Associate values with radio buttons. 
     values[radioButton1] = 0.9; 
     // repeat the same for others... 

     // Associate values messages with group boxes. 
     messages[groupBox1] = "Oda Tipi girilmedi."; 
     // repeat the same for others... 
} 

#region GroupBox.Enter event handlers. 

private void groupBox1_Enter(object sender, EventArgs e) 
{ 
    RadioButton radioButton = GetSelectedRadioButton(sender as GroupBox); 
    if (radioButton == null) 
    { 
     MessageBox.Show(messages[sender as GroupBox]); 
    } 
} 

// Here you can either repeat the same for other group boxes 
// or simply assign this event hander to all of them. 
// It will get the right message for each group. 

#endregion 

// Gets the selected radio button from the specified group. 
private void RadioButton GetSelectedRadioButton(GroupBox groupBox) 
{ 
    RadioButton radioButton = 
     groupBox 
     .Controls 
     .OfType<RadioButton>() 
     .Where(rb => rb.Checked) 
     .FirstOrDefault(); 
    return radioButton; 
} 

// Gets selected value from the specified group. 
private double GetSelectedValue(GroupBox groupBox) 
{ 
    RadioButton radioButton = GetSelectedRadioButton(groupBox); 
    if (radioButton == null) 
    { 
     // Nothing selected yet. 
     return double.NaN; 
    } 
    else 
    { 
     // Get the value from the dictinary. 
     return values[radioButton]; 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    // Get the selected values. 
    double value1 = GetSelectedValue(groupBox1); 
    double value2 = GetSelectedValue(groupBox2); 
    double value3 = GetSelectedValue(groupBox3); 
    double value4 = GetSelectedValue(groupBox4); 

    // Check other values in the same way. 
    if (double.IsNaN(value1)) 
    { 
     MessageBox.Show(message[groupBox1]); 
    } 

    textBox5.Text = Convert.ToString(
    value1 
    * value2 
    * value3 
    * value4 
    * (Convert.ToDouble(textBox2.Text)) 
    * (Convert.ToDouble(textBox3.Text)) 
    * (Convert.ToDouble(textBox4.Text))); 

} 
+0

@orcanyedal, если в будущем у вас будет больше переключателей, вы можете принять более общее решение. См. Обновленный код. – t3chb0t

Смежные вопросы