VBA, User Forms, Labels and TextBoxes

This article will discuss the main functionalists of the label and textbox objects used in the user form. Labels are basically used for text that the user can’t modify while textboxes are used to get input from the user. You can download the codes and files related to this article here.

Jump To:

Contents

Textboxes and Labels, Getting and Setting Text:

The application below sets the label’s text to the same text as the textbox every time the run button is pressed:

Set and Get TextBox and Label Text

Set and Get TextBox and Label Text(2)

'btnRun: Button Name
Private Sub btnRun_Click()

'lbl1: Label Name
'txt1: Textbox Name
lbl1.Caption = txt1.Text

End Sub

MultiLine TextBoxes:

The default textbox only has one line. In order to allow multiple lines of text you would either have to change its property manually, or use the code below:

Multiline TextBoxes

Setting the textbox Multiline Property to true

In the code below the multiline property of the textbox is set to true and false using the check box:

MultiLine TextBoxes(2) MultiLine TextBoxes(3)

Private Sub CheckBox1_Click()
    'if the check box is checked set multiline property to true
    If CheckBox1.Value = True Then
        TextBox1.MultiLine = True
    'if the checkbox is unchecked set the multipline property to false
    Else
        TextBox1.MultiLine = False
    End If
End Sub

TextBox Text Change Event, TextBox1_Change():

This event is triggered whenever the text inside the textbox is changed. The following code changes the label caption based on the text inside the textbox whenever the text is changed:

Textbox change Event
Textbox change Event

Private Sub TextBox1_Change()
Label1.Caption = TextBox1.Text
End Sub

TextBox Selected Text, Selected Text Length, Selected Text Start  And Selected Text Change Event:

Firstly there actually doesn’t exist a selection_change event for textboxes. Instead you could use the Textbox_MouseMove() event. This event is thrown when the mouse moves over the text box. If you need to check if something is actually selected you could use the TextBox1.SelLength Property. TextBox1.SelStart returns the position of the cursor when nothing is selected. When something is selected TextBox1.SelStart returns the start index of the selected text.  The TextBox1.SelText property returns the currently selected text in the textbox. In the code below there exists a textbox. As the selected text is changed, the selected text, the starting index of the selected text and the length of the selected text are printed on the labels:

This is what is loaded when you start the application. Since nothing is selected the TextBox1.SelStart property returns the location of the cursor:

Displaying the current index of the cursor VBA

Again nothing is selected therefore the TextBox1.SelStart property returns the location of the cursor:

VBA, GUI, Displaying the current index of the cursor

By selecting a portion of the text, the selected portion, selected length and the starting index of the selected text is displayed: VBA, GUI, Selected text details being show (selected text, selection length and selection start)

'event is triggered when ever the mouse moves over the text box
Private Sub TextBox1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    'displays the selected text in the textbox
    Label4.Caption = TextBox1.SelText
    'displays the length of the selected text of the text box
    Label5.Caption = TextBox1.SelLength
    'if nothing is selected in the textbox displays the current
    'position of the cursor.
    'if something is selected in the textbox, displays the starting
    'index of the selected text.
    Label6.Caption = TextBox1.SelStart
End Sub

Enable Disable Textboxes, (Textbox.Enabled) and Color Them Accordingly (Textbox.BackColor):

You might have a userform that based on the values the user chooses some fields must be disabled. Disabling controls can be done by setting the enabled property to false. There is only one problem, if you are using VBA the disabled textboxes will have the same color as an enabled component.  This might confuse the user. You will have to change the color of the disabled textboxes manually.The code below enables and disables textboxes on the user form based on the radio button selected. It also changes their color:

Enable, Disable Text Boxes and Change Color Accordingly

By selecting the bottom radio button the top text box is disabled and recolored and the bottom text boxes are enabled and recolored:

Enable, Disable Text Boxes and Change Color Accordingly(2)

Private Sub OptionButton1_Click()
'Top text box is enabled and recolored white
TextBox1.Enabled = True
TextBox1.BackColor = SystemColorConstants.vbWindowBackground

'bottom textboxes are disabled and recolored gray
TextBox2.Enabled = False
TextBox2.BackColor = SystemColorConstants.vb3DLight
TextBox3.Enabled = False
TextBox3.BackColor = SystemColorConstants.vb3DLight
End Sub

Private Sub OptionButton2_Click()
'top text box is disabled and recolored gray
TextBox1.Enabled = False
TextBox1.BackColor = SystemColorConstants.vb3DLight

'bottom textboxes are enabled and recolored white
TextBox2.Enabled = True
TextBox2.BackColor = SystemColorConstants.vbWindowBackground
TextBox3.Enabled = True
TextBox3.BackColor = SystemColorConstants.vbWindowBackground
End Sub

You can download the codes and files related to this article here.

If you need assistance with your code, or you are looking to hire a VBA programmer feel free to contact me. Also please visit my website  www.software-solutions-online.com

Leave a Reply

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