|
Besides the Enabled and Visible properties, TextBoxes can have the Locked property set, which prevents the user from editing the Value.
If the user will be restricted to a certain number of typed responses, other controls may be better choices, such as ListBox for text and SpinButton for numeric values.demo
EXAMPLE
From the Lesson 2 sheet of the file UserForms Lesson 2 2007.xlsx click the “Start Label and TextBox Demos” button for Label, TextBox, and CommandButton examples. The caption texts explain themselves.
CommandButtons
CommandButtons are clicked by the user to initiate actions. Like labels, they also are set up at design time. Run, Go, Exit, Clear, and OK are typical button functions.
Sometimes it is expedient to have the event handler just call a macro located in a standard module. This is easier to maintain if the same macro is to be called from different UserForms.
In the UserForm module:
Private Sub CommandButton1_Click()
Debug.Print CommandButton1.Caption
Call RunComplicatedMacro
End Sub
In a standard module:
Private Sub RunComplicatedMacro ()
Debug.Print “ComplicatedMacro”
End Sub
ToggleButtons
A ToggleButton is a special case CommandButton, used to choose between two or three conditions and display which condition is active. ToggleButton has a state associated with it, in the Value property. The state is typically Boolean, but a TripleState property is also available. If TripleState is True, then the three values of the Value Property are False, True, and Null.
The appearance of the ToggleButton changes when it is pressed, but code is typically used to change the Caption as well. The UserForm_Initialize code sets Caption to “Done” to start the button with the correct text.
Private Sub ToggleButton1_Click()
With ToggleButton1
Debug.Print .Caption
If .Value = False Then
.Caption = "Done"
Else
.Caption = "In Process"
End If
End With
End Sub
Private Sub UserForm_Initialize()
ToggleButton1.Caption = "Done"
End Sub
ListBox
A ListBox can display a specified number of rows of data without user intervention. The MultiSelect property can be set so that a user can select more than one item at a time. A user cannot directly enter any new values into the ListBox.
EXAMPLE
Lesson 2 sheet of the file UserForms Lesson 2 2007.xlsx, click the “Start ListBox and ToggleButton Demos” button for ListBox and ToggleButton examples.
ListBox Demo
1. ListBox is initialized with MultiSelect set to False.
2. Click different entries to see only one record can be selected.
3. Repeat with <Ctrl> pressed and see that selection behavior is unchanged.
4. Repeat with <Shift> pressed and see that selection behavior is unchanged.
5. In the selection mode ListBox, click “Multi select.”
6. Repeat steps 2-4, noting that any combination of multiple entries can be selected and de-selected.
7. In the selection mode ListBox, click “Extended select.”
8. Click different entries and verify only one record can be selected. Simple clicking has the same behavior as when MultiSelect is set to False.
9. Click different entries with <Ctrl> pressed and see that selection behavior is the same as with MultiSelect set to True.
10. Click the second entry, press <Shift>, and click the fifth entry. See that it selects, along with all the entries in between.
ToggleButton Demo
11. Click the “mode selection Enabled” button. See that the caption changed to “mode selection Disabled”
12. In the selection mode ListBox, click “Multi select.” Note that the selection mode can’t be changed.
13. Click the “mode selection Disabled” button. See that the caption changed to “mode selection Enabled”
14. In the selection mode ListBox, click “Multi select.” Note that the selection mode changes.
Properties Unique to ListBox and ComboBox
ListBoxes and ComboBoxes can display multiple columns. Properties ColumnCount and ColumnWidths control the display of the columns. The RowSource property contains the range of cells to populate the list. The list may be also be populated from an array and with the Add Item Method. These will be covered later.
ComboBox
A ComboBox will only display one row of data at any one time unless the user selects the drop arrow on the right of the ComboBox. The rows of data can then be between 1 and the total number of rows in the ComboBox. The default is eight, and is set by the ListRows Property of the ComboBox.
A ComboBox is so called because it combines the features of two other controls, the TextBox and the ListBox. While it will allow a user to select an existing entry from a list of entries, as with a ListBox, it will also allow the user to enter a new entry, as with a TextBox. MultiSelect is not available for the ListBox part of the control..
As with TextBox, the ControlSource property holds the cell address associated with the Value property. If the list has multiple columns, ControlSource defaults to column 1, but may be changed by setting BoundColumn. If the list has multiple columns, only column 1 is displayed.
EXAMPLE
Lesson 2 sheet of the file UserForms Lesson 2 2007.xlsx has two ComboBox examples. The first has headers on the drop-down list, and the second doesn’t. The following will work for both examples.
1. Click and drag the UserForm to the right to expose the Demo Database cells.
2. Click the drop-down arrow of the unlabeled ComboBox. Seven rows of data will be displayed, along with field headers.
3. Select one of the records and click. The fields will be displayed.
4. Click Edit. Change one of the fields.
5. Click Cancel Edit. The record will clear from the UserForm, and the record will be unchanged.
6. Repeat Steps 3 and 4. Click Save and see the changed field reflected in the list. If “ComboBox Demo with headers” is running, then the change will also be in the cell contents.
7. Enter something in each field (Wins field is not checked to be a number)
8. Click Add New. See the new record added to the list, and as above, in the cell contents. If the cell contents changed, then the list will have been resorted.
9. Repeat steps 7 and 8.
10. Select the record added in step 8.
11. Click Delete. Verify in the list that the record is removed from Demo Database, and as above, in the cell contents.
Scrollbar
The ScrollBar is seldom used as an independent control, since both ListBoxes and ComboBoxes will automatically show horizontal and vertical scrollbars if needed to show the data they contain.
|
OzGrid.com accepts no responsibility for any adverse affect that may result from undertaking our training.