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
|CheckBox||chb||value||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||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||cmb||caption||Creates a button the user can choose to carry out a command.|
|Frame||fra||caption||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||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||Allows you to have text that you do not want the user to change, such as a caption under a graphic.|
|ListBox||lsb||value||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.|
|MultiPage||mlp||Presents multiple screens of information as a single set.|
|OptionButton||opt||Allows you to display multiple choices from which the user can choose only one.|
|ScrollBar||scr||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.|
|SpinnerButton||spn||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||Allows you to define multiple pages for the same area of a window or dialog box in your application.|
|TextBox||txt||text||Holds text that the user can either enter or change.|
|ToggleButton||tog||Creates a button that toggles on and off.|
What is the difference between a tab control and a multipage control ??
A common property available for a lot of the controls is WordWrap. This will allow you to have more than one line of text (eg on a command button).
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 Proeprty 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.
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
Public WithEvents TextBox As MSForms.TextBox
Public UserForm As MSForms.UserForm
Private Sub TextBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox.Text = ""