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
CheckBoxchbvalueCreates 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.
ComboBoxcbovalueAllows 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.
CommandButtoncmbcaptionCreates a button the user can choose to carry out a command.
FramefracaptionAllows 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 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.
LabellblcaptionAllows you to have text that you do not want the user to change, such as a caption under a graphic.
ListBoxlsbvalueUse 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.
MultiPagemlp Presents multiple screens of information as a single set.
OptionButtonopt Allows you to display multiple choices from which the user can choose only one.
ScrollBarscr 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.
SpinnerButtonspn 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 Allows you to define multiple pages for the same area of a window or dialog box in your application.
TextBoxtxttextHolds text that the user can either enter or change.
ToggleButtontog 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).


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 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.


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



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Next