Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Button that deletes several controls

I am trying to implement a delete button that deletes several other controls such as textbox and combobox that are related to the button. Right now, I succeeded only by deleting one control by using tag function as follows:

private void deleteControl(object sender, MouseEventArgs e)
{
   Button btn = (Button)sender;

   TextBox txtbox = (TextBox)this.Controls.Find(btn.Tag.ToString(), true)[0];
   txtbox.Dispose();
}

The above code is a code snippet from my function that I implemented. However, I only can delete 1 Control by using this method since I only can tag one Control to my delete button. So how should I implement if I want to delete 2 controls using a delete button?

like image 539
Pow4Pow5 Avatar asked Nov 27 '25 08:11

Pow4Pow5


1 Answers

Try this; Iterate through available controls and delete Delete according to the condition

foreach (Control ctrl in this.Controls.OfType<Control>().ToList())
{
    if ((ctrl.GetType() == typeof(TextBox) || ctrl.GetType() == typeof(ComboBox))
        && ctrl.Tag.ToString() == btn.Tag.ToString())
    {
        ctrl.Dispose();
    }
}
like image 153
sujith karivelil Avatar answered Nov 28 '25 22:11

sujith karivelil