I have code that generates textboxes when the user right-clicks on a panel. I need a way to allow the user to also remove/delete the textbox control that was created. This code is how I create the textboxes during runtime:
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point cp = panel1.PointToClient(Cursor.Position);
i++;
TextBox text = new TextBox();
text = new TextBox();
text.Name = "user_textbox" + i.ToString();
text.Size = new System.Drawing.Size(panel1l.Width, 30);
text.Location = new Point(0, cp.Y); // puts box at current mouse position
panel1.Controls.Add(text);
text.Focus();
}
I found code to remove controls in another post, but it isn't working for what I need. That code is below, but it is designed to search and delete the control based on the name. What I'm hoping to be able to do is right-click on the textbox that was created, and have an option to delete it. Any help would be appreciated.
// this is code to remove controls using
// name of the control
foreach (Control ctrl in this.Controls)
{
if (ctrl.Name == "Textbox2")
this.Controls.Remove(ctrl);
}