Label

A Label control displays descriptive text such as titles, captions, or brief instructions. It is usually associated with some other control in the Excel Userform. We often have a caption next to a text box, combo box, or list box, but may also be on its own to provide certain information in some place of the form. We can format the font name, size, or style, the color, the position, borders and other properties of the label. The Label control has just a few events compared to other controls (or the Userform). The toolbox symbol for a Label control is highlighted in the image below.



Label Properties

The most commonly used (and default) property of the label is the Caption property. The name and caption of the label is by default Label1, Label2, etc. Do not confuse “Name” with “Caption”, the first being the name of the Label control and representing an object. We usually set the caption to each label at design time in the properties window.

 


We can also set the caption at run-time from within the Userform module (in the Initialize procedure – see Userform events), or from a standard module, using the name of the label (as object). See below the code to set the caption of a label with code inside the Userform and in a standard module.


In Userform module (as an example: Userform_Initialize procedure):

 Private Sub UserForm_Initialize()
     Me.Label1.Caption = "New caption"
 End Sub

 

In standard module:

 Sub ShowForm()
     With UserForm1
         .Label1.Caption = "Updated caption"
        .Show
     End With
 End Sub

 

We could set any other property of the label such as back or fore color, border color and style, font name, size, or style, position (with Left and Top), etc, in the same way. We may need to get or set label properties at run-time when adding labels programmatically. Otherwise, they are set at design-time in the properties window.

 

Label Events

The Label control has just a few events. We can see the list of events and add an event procedure in the code window from the drop-down on the top-right when having selected a Label object on the left.



We may want to add a Click or DblClick event to the label if used as a link, for example. It would be the same as using a button. The label may be previously formatted as a link by setting the fore color and font style properties to actually look like a link. We can also change the cursor to have a pointer instead of north-west arrow by setting the MousePointer property to fmMousePointerCustomer and adding the pointer image (a hand pointing a finger) under MouseIcon (see later Userform examples). The following procedure would use the label as a link to open a website.

 Private Sub Label1_Click()
     Dim URL As String
     URL = "https://sites.google.com/view/excelmacromania"
     ThisWorkbook.FollowHyperlink URL
 End Sub


The Label control also recognizes mouse-related events. We may want to change the color or do something else when moving the mouse (hovering) over the label.  We can also react to a mouse click while pressing down and up the mouse button. Using the following two event procedures would change the color of the label when clicking on it. This could be implemented to show that the user has properly clicked on the label. Note that if the Click (or DblClick) event is used, that would trigger after MouseDown, and before MouseUp.

 Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     Label1.ForeColor = vbRed
 End Sub
 
 Private Sub Label1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     Label1.ForeColor = vbBlack
 End Sub
 

The label control is often associated with a text box (also a combo box or list box). In the next section we take a closer look at the TextBox control.

 

 <<< Previous | Next >>>


No comments:

Post a Comment

Popular Posts