Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove a button dynamically in C#?

Tags:

c#

i have added a button and number of textBox dynamically in my C# winform app. how can i remove them dynamically? specially if i have number of same controls?

like image 1000
salman Avatar asked Nov 26 '10 14:11

salman


People also ask

How to remove dynamic button in c#?

You can call someContainer. Controls. Remove(someControl) . Alternatively, you can just Dispose() the control.

How do you add and remove controls at runtime?

To add a control to a collection programmaticallyCreate an instance of the control to be added. Set properties of the new control. Add the control to the Controls collection of the parent control. The following code example shows how to create an instance of the Button control.

How do you clear a control in C#?

Pass the Clear method a control type (TextBox, Label... etc) and a control collection, and it will clear all text from controls that implement ITextBox.


2 Answers

You need to remove the reference from the Controls collection that holds them.

frm.Controls.Remove(button1);
like image 136
Oded Avatar answered Oct 07 '22 01:10

Oded


You can call someContainer.Controls.Remove(someControl).

Alternatively, you can just Dispose() the control.

like image 36
SLaks Avatar answered Oct 07 '22 02:10

SLaks