[英]Getting value from radiobutton for textbox in C#
我在C#中有一个表单应用程序。 我需要在文本框中输入单选按钮的值。我知道方法是私有的,我将其更改为公共,但没有用。 我需要按下按钮以获取value1,value2,value3和value4。 有人可以告诉我在文本框中获取值的方法吗?
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)));
}
您可以将变量( value1
等)移至类范围,也可以将所有内容放入@@ SteveDanner建议的button1_Click
事件处理程序中,或者可以编写更通用的解决方案。 如果您创建更多选项( 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)));
}
问题在于,从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)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.