VB.NET Label Control

In VB.NET, a label control is used to display descriptive text for the form in control. It does not participate in user input or keyboard or mouse events. Also, we cannot rename labels at runtime. The labels are defined in the class System.Windows.Forms namespace.

Let's create a label in the VB.NET Windows by using the following steps:

Step 1: We have to drag the Label control from the Toolbox and drop it on the Windows form, as shown below.

VB.NET Classes and Object

Step 2: Once the Label is added to the form, we can set various properties to the Label by clicking on the Label control.

VB.NET Label Properties

PropertiesDescription
AutoSizeAs the name defines, an AutoSize property of label control is used to set or get a value if it is automatically resized to display all its contents.
Border StyleIt is used to set the style of the border in the Windows form.
PreferredWidthIt is used to set or get the preferred width for the Label control.
FontIt is used to get or set the font of the text displayed on a Windows form.
PreferredHeightIt is used to set the height for the Label Control.
TextAlignIt is used to set the alignment of text such as centre, bottom, top, left, or right.
ForeColorIt is used to set the color of the text.
TextIt is used to set the name of a label in the Windows Form.
ContextMenuIt is used to get or sets the shortcut menu associated with the Label control.
DefaultSizeIt is used to get the default size of the Label control.
ImageIt is used to set the image to a label in Windows Form.
ImageIndexIt is used to set the index value to a label control displayed on the Windows form.

VB.NET Label Events

EventsDescription
AutoSizeChangedAn AutoSizeChanged event occurs in the Label control when the value of AutoSize property is changed.
ClickClick event is occurring in the Label Control to perform a click.
DoubleClickWhen a user performs a double-clicked in the Label control, the DoubleClick event occurs.
GotFocusIt occurs when the Label Control receives focus on the Window Form.
LeaveThe Leave event is found when the input focus leaves the Label Control.
TabIndexChangedIt occurs when the value of Tabindex property is changed in the Label control.
ControlRemovedWhen the control is removed from the Control.ControlCollection, a ControlRemoved event, occurs.
TabStopChangedIt occurs when the property of TabStop is changed in the Label Control.
BackColorChangedA BackColorChanged event occurs in the Label control when the value of the BackColor property is changed.
ControlAddedWhen a new control is added to the Control.ControlCollection, a ControlAdded event occurs.
DragDropA DragDrop event occurs in the Label control when a drag and drop operation is completed.

Furthermore, we can also refer to the VB.NET Microsoft documentation to get a complete list of Label properties and events.

Let's create a program to display the Label controls in VB.NET.

Label.vb

Output:

VB.NET Classes and Object

We have created 5 Labels on the Windows Form by using drag and drop operation in the above output.

Example2: Write a program to display only Labels on Windows forms.

Form1.vb

Output:

VB.NET Classes and Object

We can also load an image in the Label Control by using the following statement in the program.






Latest Courses