|
Tabbing
Users often navigate to the next control with the Tab key.
From the VBE select the UserForm, then the View Object view. Press F4 to bring up the Properties window. Select each control and set TabStop to False for those controls not in the Tab sequence and to True for those controls to be in the Tab sequence. Select the controls in the Tab sequence in order, beginning with the first one. Set TabIndex to 0 for the first control, and increment by 1 for the rest of the tabbed controls. The controls not in the Tab sequence will have TabIndex set to numbers greater than the last control in the sequence, but it won’t matter because TabStop is set to False.
EXERCISE
This exercise will create a UserForm, add several controls, rename and re-label many of them, then arrange the layout using the various tools. The code to make the controls functional will be provided.
Size the labels:
Size the ListBoxes:
Position labels and ListBoxes:
Size and position the buttons:
Swap the middle and bottom ListBoxes:
Establish Tab order:
Resolve a naming problem:
Add code and try it:
For comparison, click the “Initialization Demo” button. Form and function should be the same as the above exercise, though the look will be different because a different developer did the layout.
Starting and Stopping UserForm
There are two methods to start a UserForm and two methods to stop it. The difference is whether the UserForm is in memory or not.
Load will create the UserForm in memory but leave it hidden to the user. Show will unhide the UserForm if it is already loaded, and will also load it if it is not already in memory.
Unhide just hides the UserForm from the user, but leaves it in memory. Unload also removes it from memory.
The Initialization event only occurs with Load, whether by itself or as part of Show.
A UserForm that is in memory can be accessed by code even though it is hidden from the user. If it has been kept in memory by Hide, it will not be re-initialized by Show.
Initializing UserForm
The default behavior when a UserForm is started is for it to retain focus as long as it is open. Since Excel 2000, UserForms can be set to allow shifting focus to the Excel file or even to other windows on the desktop. This feature is enabled when the UserForm is started, by setting
The default behavior is initiated by
Sub StartUserForm ()
UserForm1.Show
End Sub
While the feature to allow changing focus is initiated by
Sub StartUserForm ()
UserForm1.Show vbModeless
End Sub
Initializing UserForm Controls
Generally all controls are initialized the same each time the UserForm is started. The most important property is the Value property, but any property which may change during the use of the control must also be initialized. For example, location, size, background color, font, and the Visible property are often manipulated during use. Each property is set to match the vision of the application as described in Lesson 1.
Except for ComboBox and ListBox, the Value property can be set either by setting the control Value property or by changing the ControlSource cell. There may be a situation where the UserForm needs to be initialized to the values of the previous usage. This can be done simply by not updating the ControlSource cells.
Control |
Initialize Property |
Code Statement |
Label |
Caption (Usually set at design time) |
(.Caption = "x") |
TextBox |
Value or Text (Blank or <comment telling what to enter>) |
.Value = "x", or .Text = ”x” |
ListBox |
List of selectable items |
see below |
ComboBox |
List of selectable items |
see below |
CheckBox |
Value (Typically all are unselected with False |
.Value = False or True |
Frame |
Caption (not initialized) |
(.Caption = "x") |
OptionButton |
Value (Select none or one item with True) |
.Value = False or True |
CommandButton |
Caption (not initialized) |
(.Caption = "x") |
ToggleButton |
Value (False for not pressed or True for pressed) |
.Value = False or True |
TabStrip |
Initialize each tab (select with Value) |
.Value = 0 (first tab) |
MultiPage |
Initialize each page (select with Index) |
.Index = 0 (first page) |
ScrollBar |
Min, Max, Value |
.Min = , .Max = .Value = |
SpinButton |
Min, Max, Value |
.Min = , .Max = .Value = |
Image |
Picture (Usually set at design time) |
(.Picture = LoadPicture(fullpath)) |
Initializing ComboBox and ListBox Lists
Initializing ComboBox and ListBox are more complex because of the multi-row list and the possibility of multiple columns. Initialization is needed both for the list, and what items in the list are selected, if any.
ListBox1.RowSource = "'Lesson 2'!C14:E20"
If ColumnHeads is set to True, then the list headings are taken from the row above the RowSource range.
Headings in combo boxes appear only when the list drops down.
For x% = 1 To 3
ListBox1.AddItem ("Item " & x%)
Next x%
The array can be populated by code, or can be drawn from a range of cells.
Dim ary ' ary must be data type Variant.
' Variables not explicitly typed are Variant by default
ary = Array("x", "y", "z")
' or
ary = Sheets("Lesson 2").Range("C14:E20")
LIST EXAMPLE
Dim ary as Variant
ary = Array("x", "y", "z")
ListBox1.List = ary
results in ListBox1 populated with a single row
x y z
COLUMN EXAMPLE
Dim ary as Variant
ary = Array("x", "y", "z")
ListBox1.Column = ary
results in ListBox1 populated with a single column
x
y
z
ListBox1.List(0, 1)
refers to the first row, second column, and
ListBox1.Column(0, 1)
refers to the first column, second row.
Initializing ComboBox and ListBox Selection
Where a single selection is allowed, the ListIndex property identifies a selected item. ListIndex begins with 0, and the maximum is Count – 1.
Where multiple selections are allowed, the Selected property of each item identifies all the selected items. ListIndex only identifies the item with the focus.
Formatting
Formatting a UserForm to look great is not that hard! This is a bit like those fantastic looking charts that really tell us nothing. The polishing of a UserForm to look nice can be time-consuming, so don’t waste time doing it before functionality has been verified.
Colors, borders, shadows, and other appearance variations should be used sparingly. Pick a standard for the project, and vary from it only for a purpose, such as highlighting problems. In general, default to common Excel practices. The client is accustomed to seeing a certain user interface, and the closer the design can be to the user’s experience, the easier and more intuitive it will be to use. For example, Excel menus don't present a multitude of colors. So start with plain grays and other defaults.
Initializing TabStrip and MultiPage
Since TabStrips and MultiPages have their own sets of controls, they can be thought of as mini UserForms embedded in a top-level UserForm. Each control on them must be initialized just it would have if it were on a UserForm.
Both these controls display records from a database. Populating occurs every time there is a change of record. All the code to populate a page or a tab must be in a self-contained procedure, which is called each time. Initialization is simply selecting the first page or tab and calling the populating procedure.
Examples of code to handle TabStrip and MultiPage are found in the code for UserForm_Lesson03b called by the TabStrip and MultiPage Demos button on the Lesson 3 sheet of UserForms Lesson 4 2007.xlsx
|
OzGrid.com accepts no responsibility for any adverse affect that may result from undertaking our training.