* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
4.5 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Add to or Remove from a Collection of Controls at Run Time | Learn how to add controls to and remove controls from any container control on your forms, such as the Panel or GroupBox control, or even the form itself. | 03/30/2017 |
|
|
771bf895-3d5f-469b-a324-3528f343657e |
How to: Add to or Remove from a Collection of Controls at Run Time
Common tasks in application development are adding controls to and removing controls from any container control on your forms (such as the xref:System.Windows.Forms.Panel or xref:System.Windows.Forms.GroupBox control, or even the form itself). At design time, controls can be dragged directly onto a panel or group box. At run time, these controls maintain a Controls collection, which keeps track of what controls are placed on them.
Note
The following code example applies to any control that maintains a collection of controls within it.
To add a control to a collection programmatically
-
Create an instance of the control to be added.
-
Set properties of the new control.
-
Add the control to the
Controlscollection of the parent control.The following code example shows how to create an instance of the xref:System.Windows.Forms.Button control. It requires a form with a xref:System.Windows.Forms.Panel control and that the event-handling method for the button being created,
NewPanelButton_Click, already exists.Public NewPanelButton As New Button() Public Sub AddNewControl() ' The Add method will accept as a parameter any object that derives ' from the Control class. In this case, it is a Button control. Panel1.Controls.Add(NewPanelButton) ' The event handler indicated for the Click event in the code ' below is used as an example. Substite the appropriate event ' handler for your application. AddHandler NewPanelButton.Click, AddressOf NewPanelButton_Click End Subpublic Button newPanelButton = new Button(); public void addNewControl() { // The Add method will accept as a parameter any object that derives // from the Control class. In this case, it is a Button control. panel1.Controls.Add(newPanelButton); // The event handler indicated for the Click event in the code // below is used as an example. Substitute the appropriate event // handler for your application. this.newPanelButton.Click += new System.EventHandler(this. NewPanelButton_Click); }
To remove controls from a collection programmatically
-
Remove the event handler from the event. In Visual Basic, use the RemoveHandler Statement keyword; in C#, use the -= operator.
-
Use the
Removemethod to delete the desired control from the panel'sControlscollection. -
Call the xref:System.Windows.Forms.Control.Dispose%2A method to release all the resources used by the control.
Public Sub RemoveControl() ' NOTE: The code below uses the instance of ' the button (NewPanelButton) from the previous example. If Panel1.Controls.Contains(NewPanelButton) Then RemoveHandler NewPanelButton.Click, AddressOf _ NewPanelButton_Click Panel1.Controls.Remove(NewPanelButton) NewPanelButton.Dispose() End If End Subprivate void removeControl(object sender, System.EventArgs e) { // NOTE: The code below uses the instance of // the button (newPanelButton) from the previous example. if(panel1.Controls.Contains(newPanelButton)) { this.newPanelButton.Click -= new System.EventHandler(this. NewPanelButton_Click); panel1.Controls.Remove(newPanelButton); newPanelButton.Dispose(); } }