Reputation: 21
I have a form application in C#. I need to take radiobutton values in textbox.i know the methods are private, i change public but it didnt work. i need to take value1,value2,value3 and value4 with button press. Can anybody tell me a way for getting values in textboxs....
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)));
}
Upvotes: 2
Views: 4990
Reputation: 18665
You can either move the variables (value1
etc.) to the class scope or put everything in the button1_Click
event handler like @SteveDanner has suggested or you may write a more generic solution. It can be very easily extendend if you create more options (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)));
}
Upvotes: 2
Reputation: 22158
The problem is that your values being created from the RadioButtons
are local variables to the method handlers. You need to remove your groupBox_Enter
handlers and just handle the button1_Click
event like so:
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)));
}
Upvotes: 0