Controls
When a dialog box or userform is displayed to the user, the user interacts by using various controls.
The most common controls are button, option button, listbox and edit box.
It is possible to create your own custom dialog boxes by adding controls to a userform.
This section covers all the different controls which are available.
There is a different section dedicated to Userforms
Control | Prefix | Default | Comments |
CheckBox | chb | value | This control allows the user to either select or deselect an option. Creates a box that the user can easily choose to indicate if something is true or false, or to display multiple choices when the user can choose more than one. |
ComboBox | cbo | value | This control allows the user to either select an item from the drop-down list or to enter a different value into the textbox. Allows you to draw a combination list box and text box. The user can either choose an item from the list or enter a value in the text box. |
CommandButton | btn | caption | This control allows the user to perform a specific action and will run a separate subroutine. Creates a button the user can choose to carry out a command. |
DatePicker | There was a built-in DateTimePicker control but unfortunately this only works with Office 32-Bit, not Office 365. It is possible to recreate similar functionality using a combination of other controls. This solution uses built-in controls to select and validate dates and is backwards compatible with Office 32-Bit. | ||
Frame | fra | caption | This control allows you to group other controls together, such as option buttons. Allows you to create a graphical or functional grouping for controls. To group controls, draw the frame first, and then draw controls inside the frame. |
Image | img | This control allows you to add a graphic or picture to a userform. Displays a graphical image from a bitmap, icon, or metafile on your form. Images displayed in an Image control can only be decorative and use fewer resources than a PictureBox. | |
Label | lbl | caption | This control allows you to display text to the user. This is normally only to display descriptive text. Allows you to have text that you do not want the user to change, such as a caption under a graphic. |
ListBox | lsb | value | This control allows the user to select from a list of possible choices. Use to display a list of items from which the user can choose. The list can be scrolled if it has more items than can be displayed at one time. |
ListView | lsv | This control allows the user to select from a list of possible choices. | |
MonthView | mnv | This control allows the user to select a date (or time) using a large date control. | |
MultiPage | mlp | This control can be used to create a dialog box containing several tabs, allowing the user to switch between them. Presents multiple screens of information as a single set. | |
OptionButton | opt | This control allows the user to select from a list of possible choices. Allows you to display multiple choices from which the user can choose only one. | |
ProgressBar | prg | This control indicates the progress of an action by displaying a horizontal bar. | |
RefEdit | ref | This control allows the user to select a range of cells in Excel. | |
ScrollBar | scr | This control allows the user to drag a button in order to select a value. Provides a graphical tool for quickly navigating through a long list of items or a large amount of information, for indicating the current position on a scale, or as an input device or indicator of speed or quantity. | |
SpinButton | spn | This control allows the user to select a value by clicking on two buttons either to increase or decrease the value. A spinner control you can use with another control to increment and decrement numbers. You can also use it to scroll back and forth through a range of values or a list of items. | |
TabStrip | tab | This control allows you to create several tabs of the same controls. Allows you to define multiple pages for the same area of a window or dialog box in your application. | |
TextBox | txt | text | This control allows you to display text and to allow the user to enter information. Holds text that the user can either enter or change. |
ToggleButton | tog | This control allows the user to toggle between two states. Creates a button that toggles on and off. | |
TreeView | trv | This control displays a hierarchical list of items by using collapsible nodes. |
Control Names
These names are used in the event procedures and can also be used in other parts of the program it is a good idea to give your controls meaningful names before adding the event handlers.
To change the name of the control, open the Property window and edit the field "Name"
A popular naming technique is to use a three letter prefix to identify the type of the control. The remaining letters should describe the purpose of the control or should relate to the data that the control will hold.
Common Events
Depending on the type of control will depend on the methods and properties that are available.
There are however some properties that are common to all types of control
Enter | Sent when a control receives the input focus |
Error | Sent when an error has occurred |
Exit | Sent when the control loses the input focus |
Several Controls One Event
This is a simple example that illustrates how you can use a class module to use the same event to response to several events.
This class module contains an event that clears a textbox when it is double clicked.
Note: Not all userform control events are available from inside a class module.
UserForm Class Module
Private colTextBoxes As New Collection
Private Sub UserForm_Initialize()
Dim objcontrol As MSForms.Control
Dim objControlEvent As CControlEvents
For Each objcontrol In Me.Controls
If TypeOf objcontrol Is MSForms.TextBox Then
Set objControlEvent = New CControlEvents
Set objControlEvent.TextBox = objcontrol
Set objControlEvent.UserForm = Me
colTextBoxes.Add objControlEvent
End If
Next objcontrol
End Sub
CControlEvents Class
Public WithEvents TextBox As MSForms.TextBox
Public UserForm As MSForms.UserForm
Private Sub TextBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox.Text = ""
End Sub
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext