Dynamic Controls in VB.NET

Dynamic controls are the controls that are created dynamically, such as buttons, labels, textboxes, Radio buttons, etc. at run time in the Windows forms. Each control of the VB.NET Toolbox is a member of the Control class in the System.Windows.Forms namespace. And each control in the Toolbox is a class that is similar to a Windows Forms class.

Methods of Dynamic Control

Add(): In Dynamic Control, we use the Add() method to add a new element to the current Form of the control collection.

Syntax:

Remove(): The Remove method is used to remove an element from the control collection. It takes control' object as an argument to be removed from the index of the control.

Syntax:

Clear() Method: The Clear method is used to remove all elements of the controls from the Controls array or clear the form.

Count Property: The Count property is used to count the number of dynamic controls as an element in a control collection.

All() Method: All method is used to get all dynamic controls from the Windows Form as an array of SystemWinForms.Control.

Let's create a simple program to generate Dynamic control in the VB.NET Windows Forms.

DynamicControl.vb

Output:

Dynamic Controls in VB.NET

At each click on the Dynamic Button, it shows the following message on the screen and creates a button in the Windows Form at runtime, as shown below.

Dynamic Controls in VB.NET
Dynamic Controls in VB.NET

At each click on the Dynamic TextBox button, it shows the following message on the screen and creates a TextBox in the Windows Form at runtime, as shown below.

Dynamic Controls in VB.NET
Dynamic Controls in VB.NET

At each click on the Dynamic Label button, it shows the following message on the screen and creates a Label in the Windows Form at runtime, as shown below.

Dynamic Controls in VB.NET
Dynamic Controls in VB.NET




Latest Courses