There is a way to create a sequence of "if"s with textBox and method changes programmatically (C#)?

40 Views Asked by At

I have a form with two groupBoxes with various radioButtons each; When selecting one of them, I want to change the texts in textboxes (in each groupBox);

Currently using this sequence of "if's" it's working, but there is a more elegant way to do it?

        if (radioButton_5_1.Checked == true)
        {
            this.textBox_5_0.Text = MyCommands.GetDetailValue(5, 0);
            this.textBox_5_1.Text = MyCommands.GetDetailValue(5, 1);
    ...
            this.textBox_5_8.Text = MyCommands.GetDetailValue(5, 8);
        }
        if (radioButton_5_2.Checked == true)
        {
            this.textBox_5_0.Text = MyCommands.GetDetailValue(6, 0);
            this.textBox_5_1.Text = MyCommands.GetDetailValue(6, 1);
    ...
            this.textBox_5_8.Text = MyCommands.GetDetailValue(6, 8);
        }
        if (radioButton_5_3.Checked == true)
        {
            this.textBox_5_0.Text = MyCommands.GetDetailValue(7, 0);
            this.textBox_5_1.Text = MyCommands.GetDetailValue(7, 1);
    ...
            this.textBox_5_8.Text = MyCommands.GetDetailValue(7, 8);
        }
        if (radioButton_5_4.Checked == true)
        {
            this.textBox_5_0.Text = MyCommands.GetDetailValue(8, 0);
            this.textBox_5_1.Text = MyCommands.GetDetailValue(8, 1);
    ...
            this.textBox_5_8.Text = MyCommands.GetDetailValue(8, 8);
        }


        if (radioButton_19_1.Checked == true)
        {
            this.textBox_19_0.Text = MyCommands.GetDetailValue(19, 0);
            this.textBox_19_1.Text = MyCommands.GetDetailValue(19, 1);
        }
        if (radioButton_19_2.Checked == true)
        {
            this.textBox_19_0.Text = MyCommands.GetDetailValue(20, 0);
            this.textBox_19_1.Text = MyCommands.GetDetailValue(20, 1);
        }
        if (radioButton_19_3.Checked == true)
        {
            this.textBox_19_0.Text = MyCommands.GetDetailValue(21, 0);
            this.textBox_19_1.Text = MyCommands.GetDetailValue(21, 1);
        }
  • textboxes and radioButtons with 5 are in a groupBox_0;
  • textboxes and radioButtons with 19 are in a groupBox_1;
  • The first argument of GetDetailValue vary according to the selected radioButton;

in groupBox_0:

if radioButton ends with 1, first arg = 5; if radioButton ends with 2, first arg = 6; ...

in groupBox_1:

if radioButton ends with 1, first arg = 19 if radioButton ends with 2, first arg = 20 ...

  • The second argument of GetDetailValue vary according to the related textBox;

    if textBox ends with 0, second arg = 0 if textbox ends with 1, second arg = 1 ...

1

There are 1 best solutions below

0
Tony Hopkinson On BEST ANSWER

Lots, depends on how much effort you want to put in. A quick win would be to put the start index in the tag property of the check box e.g radioButton_5_1.Tag = 5, or in the form designer...

Have to forgive me I'm a bit rusty on c# with windows now

but some thing like

void OnCheckChange(object sender, EventArgs args)
{
   RadioButton button = sender as RadioButton;
   int start = (int)button.Tag;
   if (button.Checked)
   {
      this.textBox_5_0.Text = MyCommands.GetDetailValue(start, 0);
      this.textBox_5_1.Text = MyCommands.GetDetailValue(start, 1);
    ...
      this.textBox_5_8.Text = MyCommands.GetDetailValue(start, 8);
   }
}

casting sender will get rid of the explicit names of the radio buttons, Tag gives you your start and you only need one block of code.