My problem is that when I compare two, absolutely the same tags, they turn out to be different instead.
The description of a code: I create 3 controls (button, label and a textbox) and assign the same tag (let's say, 0) to them all. When I press this newly created button, I want to delete all of the 3 controls with the same tag.
Code for adding the buttons (simplified):
int Count = 0; // This var changes, but for the example it is 0
Button newButton = new Button();
newButton.Tag = Count;
newButton.Click += new EventHandler(DeleteName);
Controls.Add(newButton);
Label newLabel = new Label();
newLabel.Tag = Count;
Controls.Add(newLabel);
And the same for a TextBox.
The coding behind deleting:
private void DeleteName(object sender, EventArgs e)
{
List<Control> toDelete = new List<Control>();
Button btn = sender as Button;
foreach (Control c in Controls)
{
if (c.Tag == btn.Tag)
toDelete.Add(c);
}
int tmp = toDelete.Count;
for (int i = 0; i < tmp; i++)
{
Controls.Remove(toDelete[i]);
}
}
It used to work perfectly when I did the same logic before, but now it just deletes the button and no other control (the textbox and label stay untouched). When I replace "if (c.Tag == btn.Tag)" with, for example, "if (c is TextBox)", it adds all the TextBoxes to the list and deletes them, so I believe the problem is in this comparison of Tags.