Disable multiple controls based upon Combo Box value

Viewed 29

I have tried a number of iterations to get this to work, and although I am close, I cannot seem to solve this.

private void cbxMealsNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int count = 0;

            int cbxCurr = cbxMealsNum.SelectedIndex+1;

            foreach (Control control in foodMeals.Controls.OfType<ComboBox>().Where(x => x.Name.Contains("MealsNo" + cbxMealsNum.Text)))
            {
                TextBox textBox = control as TextBox;
                if (count < cbxCurr)
                {
                    control.Enabled = true;
                }
                else
                {
                    control.Enabled = false;
                }
                count++;
            }
        }

The above code allows me to Enable the correct combo box but none of the combo boxes before it. As such, if I select '6' in cbxMealsNum, meal 6 is enabled, but not the 5 preceding it.

Thus, I am asking how I would change the index for cbxMealsNum to, say, 4, and have only cbxMealsNo1 through to 4 enabled. If I then change cbxMealsNum to 3, cbxMealsNo4 should be disabled. If I change cbxMealsNum to 5, cbxMealsNo4 should be enabled once more, as should cbxMealsNo5.

I have tried a number of iterations of this code, including the following answer here as seen in the above sample, but to no avail. I am new to C# but I have been looking for every possible solution I can. I do not know if my search terms are malformed.

Please note, I have used the Where method as I intend to add textboxes and other controls contain the same naming convention (thus; cbxMealsNo1, txtMealsNo1, lblMealsNo1, and so on)

sample of GUI when attempting to add 3 meals

1 Answers

I am assuming you want all the combos enabled up to the selected number in the “number of meals” combo box. If this is the case then the code below may help.

First, it may be easier to put all the combo boxes into a collection since you will be needing them each time the “number of meals” combo box changes. It seems unnecessary to “re-collect” them each time. In the example below I created a simple array of six (6) combo boxes. We can then use that array to loop through all the combo boxes and enable the proper combo boxes.

ComboBox[] combos = new ComboBox[6];
public Form1() {
  InitializeComponent();
  combos[0] = cbxMealsNo1;
  combos[1] = cbxMealsNo2;
  combos[2] = cbxMealsNo3;
  combos[3] = cbxMealsNo4;
  combos[4] = cbxMealsNo5;
  combos[5] = cbxMealsNo6;
}

Then in the “number of meals” selection changed event, a simple loop to enable all the combo boxes that are less than the selected number of meals. Something like….

private void cbxMealsNum_SelectedIndexChanged(object sender, EventArgs e) {
  int cbxCurr = cbxMealsNum.SelectedIndex;
  for (int i = 0; i < combos.Length; i++) {
    if (i <= cbxCurr) {
      combos[i].Enabled = true;
    }
    else {
      combos[i].Enabled = false;
    }
  }
}
Related