1

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

twelfth
  • 73
  • 8

1 Answers1

2

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;
    }
  }
}
JohnG
  • 9,259
  • 2
  • 20
  • 29
  • Hi @JohnG. Thanks very much for this, that solves the issue acutally! Could I just ask, if I place the array in a class `static class Globals` and assign within this class too, would that also be fine? I just would like to keep all of my global variables in one place is all. – twelfth Mar 20 '21 at 20:08
  • 1
    You may not be able to do that depending on how the global file is defined. The combo boxes `cbxMealsNo1`, 2, 3 etc… will not be “created” (exposed) until AFTER the forms `InitializeComponent` is called. In other words, if you try to define the array before the forms `intializeComponent` is called, it will not know what `cbxMealsNo1`, 2, 3, etc are. In addition, they should “only” be exposed to “that” form. Anything outside that form, the combo boxes should not be exposed. – JohnG Mar 20 '21 at 20:14
  • Apologies, I wrote that unclearly. So `static class Globals` is a subsidiary class of `public partial class Form1 : Form`, written after `public Form1()` is called. That is, it at the same level as a button or combo box within Form1. – twelfth Mar 20 '21 at 20:18
  • 1
    If it is defined at the same “form” level, then yes, adding the `combos` to your "global" variables should be fine. You will know when you try an assign it the combo boxes. If you can not “assign” them in your global file, then you CAN “define” the variable and add the combo boxes later in the constructor or the forms `Load` event. – JohnG Mar 20 '21 at 20:22
  • Great, thank you very much, that solves everything for me! – twelfth Mar 20 '21 at 20:23