Click here to Skip to main content
15,888,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm lost here, I wanna remove the item from ComboBox once it's been selected in the previous ComboBox. I have tried many approaches but didn't resolve this problem. Please help me in solving this problem and below I've included all of the code. Any tips would be greatly appreciated!

What I have tried:

C#
ComboBox[] combo_for_constraint = new ComboBox[count];
int combo_for_constraint_x = 250, combo_for_constraint_y = 50;
for (int i = 1; i < count; i++)
{
    combo_for_constraint[i] = new ComboBox();
    combo_for_constraint[i].Location = new System.Drawing.Point(combo_for_constraint_x,
combo_for_constraint_y + (i * 10));
    combo_for_constraint[i].Size = new Size(100, 50);
    combo_for_constraint[i].Name = "combo_for_constraintbx" + i.ToString();
    combo_for_constraint[i].DropDownStyle = ComboBoxStyle.DropDownList;
    tabPage2.Controls.Add(combo_for_constraint[i]);
    tabPage2.ResumeLayout(false);
    combo_for_constraint_y += 40;
    tabPage2.Refresh();
    if (i >= 2 && combo_for_constraint[i - 1].Text == "PRIMARY KEY")
    {
        combo_for_constraint[i + 1].Items.RemoveAt(1);
    }
    else
    {
        combo_for_constraint[i].Items.Add("UNIQUE");
        combo_for_constraint[i].Items.Add("PRIMARY KEY");
        combo_for_constraint[i].Items.Add("FOREIGN KEY");
    }
}
Posted
Updated 6-Jan-21 19:16pm
v2
Comments
CHill60 5-Jan-21 13:12pm    
What happens when you run the code?
One tip I will give you is don't start at 1 and go to the end of the list - when you remove an item, all the subsequent items will have a different index. So start at the end and move towards the start
Dev-Ahsan 5-Jan-21 13:28pm    
when I run the code all the ComboBox remains the same but I won't if I select "Primary Key" from the comboBox the item Primary will be removed from the ComboBox except the ComboBox in which I select. I've tried but not work
Richard MacCutchan 6-Jan-21 10:35am    
I have tried various values for count but I only ever get one ComboBox created and the program then aborts.
Richard MacCutchan 6-Jan-21 10:49am    
I have tried a number of other tests and can still not get a working sample. I think part of the problem is the way you are trying to update one of the combo boxes in the creation phase.

You should first create the combo boxes and populate them with the required data. The code to delete an item from a box should be put into an event handler that is triggered when the selection changes.
Dev-Ahsan 7-Jan-21 1:02am    
Got it, Thanks for your advice it's really helped me!

Here is some code that works, although it is not an ideal implementation. But it does at least show how the SelectedIndexChanged event should be handled.
C#
ComboBox[] combo_for_constraint;
void SetupCombos()
{
    int count = 3;
    combo_for_constraint = new ComboBox[count];
    int combo_for_constraint_x = 250, combo_for_constraint_y = 50;
    for (int i = 1; i < count; i++)
    {
        combo_for_constraint[i] = new ComboBox();
        combo_for_constraint[i].Location = new System.Drawing.Point(combo_for_constraint_x,
            combo_for_constraint_y + (i * 10));
        combo_for_constraint[i].Size = new Size(100, 50);
        combo_for_constraint[i].Name = "combo_for_constraintbx" + i.ToString();
        combo_for_constraint[i].DropDownStyle = ComboBoxStyle.DropDownList;
        this.Controls.Add(combo_for_constraint[i]);
        this.ResumeLayout(false);
        combo_for_constraint_y += 40;
        this.Refresh();
        combo_for_constraint[i].Items.Add("UNIQUE");
        combo_for_constraint[i].Items.Add("PRIMARY KEY");
        combo_for_constraint[i].Items.Add("FOREIGN KEY");
        combo_for_constraint[i].SelectedIndex = 0;
    }
    combo_for_constraint[1].SelectedIndexChanged += new System.EventHandler(Combo1_SelectedIndexChanged);
}

void Combo1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ComboBox comboBox = (ComboBox)sender;
    if (comboBox == combo_for_constraint[1] && combo_for_constraint[1].Text == "PRIMARY KEY")
    {
        combo_for_constraint[2].Items.RemoveAt(1);
        combo_for_constraint[2].DroppedDown = true;
    }
}
 
Share this answer
 
v2
Finally solved by the help of
@Richard-MacCutchanWatch

C#
void ComboBuild()
        {
            combo_for_constraint = new ComboBox[count];
            int combo_for_constraint_x = 250, combo_for_constraint_y = 50;
            for (int i = 0; i < count; i++)
            {
                combo_for_constraint[i] = new ComboBox();
                combo_for_constraint[i].Location = new System.Drawing.Point(combo_for_constraint_x,
                    combo_for_constraint_y + (i * 10));
                combo_for_constraint[i].Size = new Size(100, 50);
                combo_for_constraint[i].Name = "combo_for_constraintbx" + i.ToString();
                combo_for_constraint[i].DropDownStyle = ComboBoxStyle.DropDownList;
                tabPage2.Controls.Add(combo_for_constraint[i]);
                tabPage2.ResumeLayout(false);
                combo_for_constraint_y += 40;
                tabPage2.Refresh();
                combo_for_constraint[i].Items.Add("UNIQUE");
                combo_for_constraint[i].Items.Add("PRIMARY KEY");
                combo_for_constraint[i].Items.Add("FOREIGN KEY");
                //combo_for_constraint[i].SelectedIndex = 0;
                combo_for_constraint[i].SelectedIndexChanged += new System.EventHandler(Combo1_SelectedIndexChanged);
            }
        }


        private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            for (int i = 0; i < int.Parse(textBox3.Text); i++)
            {
                if (comboBox == combo_for_constraint[i] && combo_for_constraint[i].Text == "PRIMARY KEY")
                {
                    for (int j = 0; j < int.Parse(textBox3.Text); j++)
                    {
                        if (combo_for_constraint[j].Text == "PRIMARY KEY")
                        {
                            continue;
                        }
                        else
                            combo_for_constraint[j].Items.Remove("PRIMARY KEY");
                    }
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900