Ok. This is kind of a weird problem. I have a form in my program that I add more controls to in order to allow users to enter more data if need be. I also have a button that clears the form and resets it to its initial state. The problem is I have to push the clear button multiple times in order for all the fields I added to be removed. I am not sure why this is. Here is the loop I am using to remove the fields.
foreach (Control ctrl in grpIngredients.Controls)
{
if (ctrl.Name == "txtAmt6")
{
TextBox txtAmt6 = (TextBox)grpIngredients.Controls["txtAmt6"];
grpIngredients.Controls.Remove(txtAmt6);
}
else if (ctrl.Name == "txtAmt7")
{
TextBox txtAmt7 = (TextBox)grpIngredients.Controls["txtAmt7"];
grpIngredients.Controls.Remove(txtAmt7);
}
else if (ctrl.Name == "txtAmt8")
{
TextBox txtAmt8 = (TextBox)grpIngredients.Controls["txtAmt8"];
grpIngredients.Controls.Remove(txtAmt8);
}
else if (ctrl.Name == "txtAmt9")
{
TextBox txtAmt9 = (TextBox)grpIngredients.Controls["txtAmt9"];
grpIngredients.Controls.Remove(txtAmt9);
}
else if (ctrl.Name == "txtAmt10")
{
TextBox txtAmt10 = (TextBox)grpIngredients.Controls["txtAmt10"];
grpIngredients.Controls.Remove(txtAmt10);
}
else if (ctrl.Name == "txtAmt11")
{
TextBox txtAmt11 = (TextBox)grpIngredients.Controls["txtAmt11"];
grpIngredients.Controls.Remove(txtAmt11);
}
else if (ctrl.Name == "txtAmt12")
{
TextBox txtAmt12 = (TextBox)grpIngredients.Controls["txtAmt12"];
grpIngredients.Controls.Remove(txtAmt12);
}
else if (ctrl.Name == "txtAmt13")
{
TextBox txtAmt13 = (TextBox)grpIngredients.Controls["txtAmt13"];
grpIngredients.Controls.Remove(txtAmt13);
}
else if (ctrl.Name == "txtAmt14")
{
TextBox txtAmt14 = (TextBox)grpIngredients.Controls["txtAmt14"];
grpIngredients.Controls.Remove(txtAmt14);
}
else if (ctrl.Name == "txtAmt15")
{
TextBox txtAmt15 = (TextBox)grpIngredients.Controls["txtAmt15"];
grpIngredients.Controls.Remove(txtAmt15);
}
else if (ctrl.Name == "txtAmt16")
{
TextBox txtAmt16 = (TextBox)grpIngredients.Controls["txtAmt16"];
grpIngredients.Controls.Remove(txtAmt16);
}
else if (ctrl.Name == "cboName6")
{
ComboBox cboName6 = (ComboBox)grpIngredients.Controls["cboName6"];
grpIngredients.Controls.Remove(cboName6);
}
else if (ctrl.Name == "cboName7")
{
ComboBox cboName7 = (ComboBox)grpIngredients.Controls["cboName7"];
grpIngredients.Controls.Remove(cboName7);
}
else if (ctrl.Name == "cboName8")
{
ComboBox cboName8 = (ComboBox)grpIngredients.Controls["cboName8"];
grpIngredients.Controls.Remove(cboName8);
}
else if (ctrl.Name == "cboName9")
{
ComboBox cboName9 = (ComboBox)grpIngredients.Controls["cboName9"];
grpIngredients.Controls.Remove(cboName9);
}
else if (ctrl.Name == "cboName10")
{
ComboBox cboName10 = (ComboBox)grpIngredients.Controls["cboName10"];
grpIngredients.Controls.Remove(cboName10);
}
else if (ctrl.Name == "cboName11")
{
ComboBox cboName11 = (ComboBox)grpIngredients.Controls["cboName11"];
grpIngredients.Controls.Remove(cboName11);
}
else if (ctrl.Name == "cboName12")
{
ComboBox cboName12 = (ComboBox)grpIngredients.Controls["cboName12"];
grpIngredients.Controls.Remove(cboName12);
}
else if (ctrl.Name == "cboName13")
{
ComboBox cboName13 = (ComboBox)grpIngredients.Controls["cboName13"];
grpIngredients.Controls.Remove(cboName13);
}
else if (ctrl.Name == "cboName14")
{
ComboBox cboName14 = (ComboBox)grpIngredients.Controls["cboName14"];
grpIngredients.Controls.Remove(cboName14);
}
else if (ctrl.Name == "cboName15")
{
ComboBox cboName15 = (ComboBox)grpIngredients.Controls["cboName15"];
grpIngredients.Controls.Remove(cboName15);
}
else if (ctrl.Name == "cboName16")
{
ComboBox cboName16 = (ComboBox)grpIngredients.Controls["cboName16"];
grpIngredients.Controls.Remove(cboName16);
}
I have tried using a Switch also instead of the if/else if but get the same result. Is there any reason why this would be stopping in the middle of the loop? Thanks for the help.
P.S. This is a long code block. Is there a way to make the block smaller and add a scrollbar?