Visual Basic Tutorials – Controls – The Label

Controls The Label

label visual basic

label visual basic

The label is the basic control used to place text in a form. The main difference between a text box and label is that a user can modify text inside a text box but cannot modify the text in a label.

Typically, label is used to identify controls that do not have a caption property of their own. For example, a text box.

Label Properties:

Some properties of the label are:

Alignment Aligns caption within border.
Appearance Selects 3-D or flat appearance.
AutoSize If True, the label is resized to fit the text specified by the caption property.  If False, the label will remain the size defined at design time and the text may be clipped.
BorderStyle Determines type of border.
Caption String to be displayed in box.
Font Sets font type, style, size.
WordWrap Works in conjunction with AutoSize property.  If AutoSize = True, WordWrap = True, then the text will wrap and label will expand vertically to fit the Caption.  If AutoSize = True, WordWrap = False, then the text will not wrap and the label expands horizontally to fit the Caption.  If AutoSize = False, the text will not wrap regardless of WordWrap value.

Label Events:

How can label help on event driven programming? The label supports events such as:

Click Event triggered when user clicks on a label.
DblClick Occurs when the user double clicks with the mouse on the label
Change Occurs when the text of the label is changed by the application

Label Methods:

Few methods are associated with the Label. The label is one of the few passive elements to be associated with an interface. It is used as identification marker.

One method that is associated with a label is:

  • Move – Used to move the label.

Creating Labels on a Form

To create a label control on a form, we surely refer to the toolbox window to select the label icon, shown as capital letter “A”. When the label control is selected, the label can be placed on a form by creating a rectangle with the mouse, which is held left button clicked. Once the left button is released, the label of size as the rectangle created is placed on the form.


Creating Labels in Code

You can also set label properties with program code, as shown in the following.

The program codes below, when command1 button is clicked, will set the caption of label1 as “Welcome” and label2 as “Please enter your name below:”

Private Sub Command1_Click ()

Label1.Caption = "Welcome"

Label2.Caption = "Please enter your name below:"

lblDigitalClock.Caption = Time

End Sub

Using Labels To Give Access Keys to Controls without Captions

With easier user interface, users desire to use their program by just using Keyboard. So a programmer need to add access keys to all the button captions so the user can the button the focus just by pressing Alt + the access key (just like menu items). You think how do you give an access key to a textbox?

This is where a useful aspect of labels comes in handy. In fact, this aspect of the label control is built just to handle this problem. You can give access keys to controls with Caption properties just by placing an ampersand (&) in the caption in front of the letter you want to make the access key but how can you do that if a control (like a text box) has no Caption property?

Here’s the way you do it: you give the access key to a label control and then make sure the control you want to give the focus to with that access key is next in the tab order (that is, has the next highest TabIndex property value). Because labels cannot accept the focus themselves, this is a neat feature: when the user presses Alt + the access key, the label passes the focus on to the next control. In this way, you can give even controls without Caption properties access keys.


WARNING! When you use access keys, make sure you set the label’s UseMnemonic property to True (the default), or the access key won’t be enabled.

Access Key by label properties

Access Key by label properties

As an example, we’ve given the two labels in Figure access keys. When the user presses Alt + the access key above a text box, the focus is set to that text box, because those text boxes follow their individual labels in the tab order.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>