VB Lecture Notes - User Interface
Objective #1: Properly design the user interface using objects' TabIndex properties, the SetFocus method,
and the focus along with keyboard shortcuts and Clear buttons.
- The TabIndex Property is used to give the user a more user-friendly experience while executing the program. By
setting an object's TabIndex Property to 0, a programmer can ensure that the user can immediately enter information into that object (usually a textbox or label). For
example, a programmer may want to set the TabIndex property of a button to 0 to highlight that button to allow the user to simply press the Enter key to continue with
the execution of a program.
- Objects such as buttons and textboxes have the property called TabIndex. As you create objects and place them on the form, these objects' TabIndex properties
are set to the numbers 0, 1, 2, etc. Only one object can have a given a TabIndex property setting of 0, only one object can have a setting of 1, etc. Whichever object
has the lowest TabIndex property will first have the focus. By setting a form's objects sequential TabIndex properties (say, from
0 to 7), the programmer can lead the user in a sensible, logical manner during the program execution.
- The focus refers to the object which is selected at any given moment during a program's execution. For example, if the cursor is blinking within a text box, allowing the user
to input a number there by typing on the keyboard, that text box is said to have the focus. However, if the user presses the Tab key on the keyboard, then the focus will jump to the object that has
the next greatest TabIndex property. Once the user moves the focus to the object with the greatest TabIndex property, pressing the Tab key will cause the focus to return
to the original object with the smallest TabIndex property setting (usually 0, not 1).
- You should carefully create objects and place them on a form in the order that you wish the user to be able to access them via the Tab key. In that way, you will not have to manually
set the TabIndex properties.
- An ampersand symbol ( & ) is often used before a letter in the Text property so that the letter is underlined. This letter's key can be typed
along with the Alt key as an alternate way for the user to activate the object's Click event. For example, if the button is an Exit button, it is standard to underline
the x in the "Exit" Text property. This key combination is called a keyboard shortcut. Some people are handicapped or have little
dexterity with a mouse and prefer to type keyboard keys. It is the programmer's responsibility to allow for multiple modes of input. Doing so is called accessibility. Furthermore, you
should follow standard Windows conventions when choosing which letter to set as an object's keyboard shortcut such as the 'x' in Exit.
- You should place a Clear button as well as an Exit button when appropriate for a more user-friendly, conventional interface. This gives the user an easy way to clear out multiple textboxes or labels without having to manually clear each one separately. Your Clear button's Click method might look like this:
Private Sub btnClear_Click( )
lblStep1.Text = ""
lblStep2.Text = ""
lblStep3.Text = ""
End Sub
or, since textboxes have a Clear method, you can use
txtInput.Clear()
instead of
txtInput.Text = ""
to clear out a textbox.
- The Focus method can be used to purposefully place the focus on a certain object. For example, if you wish
the focus to be placed on the Exit button at a particular point in a program's execution so that the user only has to press the Enter key to exit the program, you would use the statement,
btnExit.Focus()