I have a dynamically created
Label
RichTextBox
Label dateLabel = new Label();
dateLabel.Text = dateArray[i];
dateLabel.Name = "date" + i;
dateLabel.Location = new Point(154, 5 + (50 * i));
dateLabel.Tag = dateLabel;
dateLabel.Size = new System.Drawing.Size(91, 20);
panel1.Controls.Add(dateLabel);
RichTextBox placeTravelLabel = new RichTextBox();
placeTravelLabel.Text = placeTravelArray[i];
placeTravelLabel.Name = "placeTravel" + i;
placeTravelLabel.Location = new Point(272, 5 + (50 * i));
placeTravelLabel.Tag = placeTravelLabel;
placeTravelLabel.Size = new System.Drawing.Size(148, 45);
placeTravelLabel.ReadOnly = true;
panel1.Controls.Add(placeTravelLabel);
Button clearButton = new Button();
clearButton.Name = "clearButton" + i;
clearButton.Text = "Remove";
clearButton.Location = new Point(1200, 5 + (30 * i));
clearButton.Click += new EventHandler(this.clearButton_Click);
panel1.Controls.Add(clearButton);
remove
public void clearButton_Click(object sender, EventArgs e)
{
dateLabel.Remove();
placeTravelLabel.Remove();
}
Yes, it is. Try
panel1.Controls.Remove(dateLabel);
panel1.Controls.Remove(placeTravelLabel);
You obviously need to hold the references to them when you create them (i.e. declare them as fields in your class) or mark them somehow (e.g. in Tag
property) and enumerate panel1.Controls
to find them later.