I have a form with dynamic added controls. When the user clicks next all dynamic controls have to be removed. My code only removes some of them.
for example dtxt2 does not get removed the first time but if I run the remove function a second time it does get removed.
This is the function to add the dynamics:
void TextboxesStep21()
{
removeDynamics();
TextBox dtxt1 = new TextBox();
dtxt1.Name = "DynamicTextBox1";
dtxt1.Location = new System.Drawing.Point(336, 125);
dtxt1.Text = ((diameterOfDrivingWeel + spindleSlope)*10).ToString();
this.Controls.Add(dtxt1);
TextBox dtxt2 = new TextBox();
dtxt2.Name = "DynamicTextBox2";
dtxt2.Location = new System.Drawing.Point(336, 148);
dtxt2.Text = gearingRatioDen.ToString();
this.Controls.Add(dtxt2);
TextBox dtxt3 = new TextBox();
dtxt3.Name = "DynamicTextBox3";
dtxt3.Text = gearingRatioNum.ToString(); ;
dtxt3.Location = new System.Drawing.Point(336, 171);
this.Controls.Add(dtxt3);
pictureBox1.SendToBack();
pictureBox2.SendToBack();
}
This is my remove function
void removeDynamics()
{
foreach (Control x in this.Controls)
{
if (x.Name.Contains("Dynamic"))
{
this.Controls.Remove(x);
x.Dispose();
}
}
}