1

I have a few comboboxes inside groupbox1 and then I have two more groupboxes with a few buttons and numericupdown controls inside each one of the two groupboxes. I need to have the controls inside groupbox2 and groupbox3 be disabled on form load. And not be enabled until the user picks a value from each one of the comboboxes inside groupbox1.

I've tied creating an if else statement inside the form load event.

if (comboBox1.SelectedIndex <= 0 && comboBox2.SelectedIndex <= 0 && comboBox3.SelectedIndex <= 0 && comboBox4.SelectedIndex <= 0)
{
groupBox2.Enabled = false;
groupBox3.Enabled = false;
}
else
{
groupBox2.Enabled = true;
groupBox3.Enabled = true;
}

When I run the code, I don't get any exceptions at all on form load all the controls inside groupBox2 and groupBox3 are disabled(grayed out) And when I change the value on comboxBox1 inside groupBox1 the controls inside groupBox3 are enabled as expected. However, the controls inside groupBox2 remain disabled and changing the value on any of the other comboBoxes does nothing at all even to groupbox3. I would appreciate any help, thanks.

gdir
  • 922
  • 2
  • 17
  • 25
Yorelis
  • 75
  • 1
  • 1
  • 5

2 Answers2

0

If the code you pasted is contained in Form.Load(), it will only execute once, upon the form being loaded.

If you want to perform these checks at runtime, it will have to be tied to your comboBox.SelectionChanged event. From your description I'm guessing you have a different code contained there, that affects groupBox2 but not groupBox3.

Also, you say that the logic should be "not enabled until the user picks a value from each one of the comboboxes". The way you constructed your if statement, it is enough that one of the comboBoxes changes its SelectedIndex to anything above 0 to enable the two groupBoxes. To achieve the logic you described, you'd have to change all the '&&' into '||'.

Artur Szpot
  • 34
  • 2
  • 6
  • wuzet - Changing all the '&&' into '||' did not make a difference and the controls inside groupbox3 are still being enabled after only one of the comboboxes gets a value change. However, placing the 'if else' statement inside one of the comboBoxes SelectedIndexChanged event did make the controls inside groupBox2 enabled, thank you for that. I still need the controls inside groupBox2 and groupBox3 to be enabled only after all the values inside all the comboBoxes have changed. How can I do that? – Yorelis Aug 08 '19 at 13:43
  • For brevity, let a,b,c,d stand for comboBox1,2,3,4.SelectedIndex. If no item is selected, SelectedIndex is -1 (see https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindex?view=netframework-4.8). I'm guessing you have "Choose..." under SelectedIndex = 0. To check if any of these is chosen, you'll want: (1) a>0 || b>0 || c>0 || d>0 To check if all are chosen, use: (2) a>0 && b>0 && c>0 && d>0 And if you'd like none chosen, that would be: (3) a<=0 && b<=0 && c<=0 && d<=0 or a negation of (1). – Artur Szpot Aug 08 '19 at 14:01
  • [Pass click event of child control to the parent control](https://stackoverflow.com/a/36130796/3110834) – Reza Aghaei Aug 08 '19 at 14:06
  • I've tried all the different combinations and some of them didn't disable the controls at all, and then a few other combinations disabled the controls but still they are only getting enabled by the one combobox selected index change. – Yorelis Aug 08 '19 at 15:38
  • You'll need to show your code - I suppose you have some logic-gone-wrong somewhere else. Strip it to leave only the parts we're discussing here, check that it still produces the issue and then upload it (e.x. to pastebin) so it's possible to analyze. – Artur Szpot Aug 09 '19 at 06:57
0

I made a simple winforms application to help you with this problem.

Here is the Interface of the Application

Application IMAGE

Here are the names that I used for the controls that I used in the code

Application Control Names IMAGE

You can disable GB_2 and GB_3 when the form loads by using the following code in the form Load event.

        private void Form_Load(object sender, EventArgs e)
    {
        GB_2.Enabled = false;
        GB_3.Enabled = false;
    }

At Runtime (As soon as the form loads)

Runtime IMAGE

Scenario 1

By Selecting "Enabled or Disabled" from the Combo Boxes in GB_1, The User can Enable or Disable GB_2 or GB_3.

This is done by using the SelectedIndexChanged event for a combo box

Cmb_GB1_1 and Cmb_GB1_2 Have similar items as follows

  1. Item 01 : Disabled
  2. Item 02 : Enabled

The Code Behind Cmb_GB1_1 and Cmb_GB1_2

private void Cmb_1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Cmb_GB1_1.SelectedIndex <=0)
        {
            GB_2.Enabled = false;
        }
        else
        {
            GB_2.Enabled = true;
        }


    }


    private void Cmb_2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Cmb_GB1_2.SelectedIndex <= 0)
        {
            GB_3.Enabled = false;
        }
        else
        {
            GB_3.Enabled = true;
        }

Scenario 2

Here GB_2 and GB_3 are only enabled when the text in all three Combo Boxes are not equal to "False"

This is done by using the SelectedIndexChanged event for a combo box

Cmb_GB4_1 , Cmb_GB4_2 and Cmb_GB4_3 Have similar items as follows

  1. Item 01 : False
  2. Item 02 : Active
  3. Item 02 : OK

The Code Behind Cmb_GB4_1 , Cmb_GB4_2 and Cmb_GB4_3

        private void Cmb_GB4_1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Enb_Dis_GB();
        GB_1_TEXT_CHANGER();
    }

    private void Cmb_GB4_2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Enb_Dis_GB();
        GB_1_TEXT_CHANGER();
    }

    private void Cmb_GB4_3_SelectedIndexChanged(object sender, EventArgs e)
    {
        Enb_Dis_GB();
        GB_1_TEXT_CHANGER();
    }

I created two methods

  1. Enb_Dis_GB() : To Enable Both GB_2 and GB_3 when none of the 3 combo Boxes have "False"
  2. GB_1_TEXT_CHANGER() : This Changes the text in Cmb_GB1_1 and Cmb_GB1_2 as necessary

The Code Behind

Enb_Dis_GB()

private void Enb_Dis_GB()
    {
        if (Cmb_GB4_1.SelectedIndex > 0 && Cmb_GB4_2.SelectedIndex > 0 && Cmb_GB4_3.SelectedIndex > 0)
        {
            GB_2.Enabled = true;
            GB_3.Enabled = true;
        }

        else
        {
            GB_2.Enabled = false;
            GB_3.Enabled = false;
        }
    }

GB_1_TEXT_CHANGER()

private void GB_1_TEXT_CHANGER()
    {
     if(Cmb_GB4_1.SelectedIndex > 0 && Cmb_GB4_2.SelectedIndex > 0 && Cmb_GB4_3.SelectedIndex > 0)
        {
            Cmb_GB1_1.Text = "Enabled";
            Cmb_GB1_2.Text = "Enabled";
        }
        else
        {
            Cmb_GB1_1.Text = "Disabled";
            Cmb_GB1_2.Text = "Disabled";
        }
    }

A Set of images Showing the program At runtime.

Scenario 1 IMAGE

Scenario 2 Case 1 IMAGE

Scenario 2 Case 2 IMAGE

lllkk02
  • 1
  • 1