There are a couple of ways to do this, but if you are going to follow a convention where you know the first part of the name and only need to pass in the number, here is something that would work
Code:
private void DeleteControl(int controlNumber)
{
foreach (Control control in this.Controls)
{
String compareString = "textBox" + controlNumber.ToString();
if (control.Name == compareString)
{
this.Controls.Remove(control);
break;
}
}
}
BTW, if you're going to loop through with a "ForEach", you NEED the "break" line so it doesn't continue to loop and start referencing null values.
I don't believe there is anyway to reference a variable by name without looping. Hope this helps . . .