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

ControlPrefixDefaultComments
CheckBoxchbvalueThis 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.
ComboBoxcbovalueThis 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.
CommandButtonbtncaptionThis 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.
FramefracaptionThis 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.
Imageimg 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.
LabellblcaptionThis 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.
ListBoxlsbvalueThis 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.
ListViewlsv This control allows the user to select from a list of possible choices.
MonthViewmnv This control allows the user to select a date (or time) using a large date control.
MultiPagemlp 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.
OptionButtonopt 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.
ProgressBarprg This control indicates the progress of an action by displaying a horizontal bar.
RefEditref This control allows the user to select a range of cells in Excel.
ScrollBarscr 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.
SpinButtonspn 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.
TabStriptab 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.
TextBoxtxttextThis control allows you to display text and to allow the user to enter information.
Holds text that the user can either enter or change.
ToggleButtontog This control allows the user to toggle between two states.
Creates a button that toggles on and off.
TreeViewtrv 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

EnterSent when a control receives the input focus
ErrorSent when an error has occurred
ExitSent 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

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext