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. This 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. A Clear button can use the textbox Clear method
to clear any textboxes on the form as in txtUserName.Clear()
- The SetFocus 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.SetFocus
- One button on a form should have its Default property set to True. This button will appear shadowed and it's Click event
will respond if the user presses the Enter key. You should also set the Cancel property to True for one button on the form. An obvious candidate for this setting would
be a btnClear or even a btnExit button. This button's Click event will execute if the user presses
the Escape (Esc) key in the upper left corner of any keyboard.