Kirikkayis
Kirikkayis

Reputation: 83

How to define condition for Radiobutton in a efficient way?

i`m trying to find a efficient way to define conditions for radio-buttons.

enter image description here

After selecting a Radio-Button i should disable or fill the text-boxes. Following an example:

 private void Transportauftrag_CheckedChanged(object sender, EventArgs e)
    {
        groupBox1.Visible = true;
        groupBox8.Visible = false;
        StartGerät.Text = "";
        StartBlockFach.Text = "";
        StartTiefe.Text = "";
        ZielGerät.Text = "";
        ZielBlockFach.Text = "";
        ZielTiefe.Text = "";
        FehlerFehler.Text = "****";
        FehlerBereich.Text = "****";
        FehlerInfo.Text = "****";
        Transportbefehl.Text = "";
        Breite.Text = "";
        Tiefe.Text = "";
        Höhe.Text = "";
        Typ.Text = "";
        Folgeauftrag.Text = "";
        Behälternummer.Text = "";
        Reserve.Text = "";
        StartGerät.Enabled = true;
        StartBlockFach.Enabled = true;
        StartTiefe.Enabled = true;
        ZielGerät.Enabled = true;
        ZielBlockFach.Enabled = true;
        ZielTiefe.Enabled = true;
        FehlerFehler.Enabled = false;
        FehlerBereich.Enabled  = false;
        FehlerInfo.Enabled = false;
        Transportbefehl.Enabled = true;
        Breite.Enabled = true;
        Tiefe.Enabled = true;
        Höhe.Enabled = true;
        Typ.Enabled = true;
        Folgeauftrag.Enabled = true;
        Behälternummer.Enabled = true;
        Reserve.Enabled = true;
    }

So now i should do this 7 times. And for this i need over 400 Line of Codes in my WindowsFormsApp-File.

Are they any better way / solution to do this?

Upvotes: 1

Views: 123

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

ok here are some suggestions. Basically if faced with such a problem then it indicates that you need an organizational structure. Otherwise one gets lost. As it appears from the picture of your GUI you already have GroupBoxes that organize this. All the labels and Textboxes inside that groupbox are in groupBox1.Controls. You can get them out in 1 blow even by type:

 List<TextBox> allTextBoxesFromGroupBox1 = groupBox1.Controls.OfType<TextBox>().ToList();

Now you can iterate over it and write the same value for each Textbox:

foreach (TextBox tb in allTextBoxesFromGroupBox1)
{
    tb.Text = "";
}

But you better make a method from it:

private void FillAllTextBoxes(GroupBox groupBox, string text)
{
    List<TextBox> allTextBoxesFromGroupBox1 = groupBox.Controls.OfType<TextBox>().ToList();

    foreach (TextBox tb in allTextBoxesFromGroupBox1)
    {
        tb.Text = "";
    }
}

Now you can simply call it and pass the necessary input:

FillAllTextBoxes(groupBoxStart, "");
FillAllTextBoxes(groupBoxZiel, "");
FillAllTextBoxes(groupBoxFehler, "****");
FillAllTextBoxes(groupBoxParameter, "");

At this point we got rid of 17 Lines and introduced only 4

Proceeding... The next step would be the enabling of the controls. It seems that you treat again all Textboxes inside the same groupbox also the same. So we can introduce another parameter for the enabling part into the method:

private void FillAllTextBoxes(GroupBox groupBox, string text, bool enable)
{
    List<TextBox> allTextBoxesFromGroupBox1 = groupBox.Controls.OfType<TextBox>().ToList();

    foreach (TextBox tb in allTextBoxesFromGroupBox1)
    {
        tb.Text = "";
        tb.Enabled = enable;
    }
}

and this allows us to remove the rest of the lines entirely without introducing new lines of code:

FillAllTextBoxes(groupBoxStart, "", true);
FillAllTextBoxes(groupBoxZiel, "", true);
FillAllTextBoxes(groupBoxFehler, "****", false);
FillAllTextBoxes(groupBoxParameter, "", true);

So now you are down at 4 lines (I see only 4 groupboxes in your screenshot) of code

Upvotes: 1

Related Questions