Click a button to perform an action
The easiest way for users to interact with your application is to provide them with a button. You can use the CommandButton control provided by Visual Basic, or use an Image control that contains graphics, such as an icon, to create your own "button".
Using CommandButton
Most Visual Basic applications have CommandButtons that allow users to perform actions by simply tapping the button. When the user selects a button, not only does the action take place, but the button appears to be pressed and released. Whenever the user clicks the button, the Click event procedure is called. Write code into the Click event procedure to perform the desired action.
There are many ways to select the CommandButton while the program is executing:
Click the button with the mouse.
Press the TAB key to move the focus to the corresponding button, and then press the SPACE key or ENTER key to select the button. See "Focus Overview" later in this chapter.
Press the CommandButton shortcut key (ALT underlined character).
Set the CommandButton's Value property to True in the code. cmdClose.Value=True
Call the CommandButton's Click event in code. cmdClose_Click
If the CommandButton is the default command button of the form, even if the focus is moved to other controls that are not CommandButton, pressing the Enter key will select the button. At design time, specify a button as the default CommandButton of the form by setting its Default property to True.
If the CommandButton is the default cancel button of the form, even if the focus is moved to other controls, the button can be selected by pressing the ESC key. At design time, specify a button as the default cancel button by setting its Cancel property to True. All of these operations cause Visual Basic to call the Click event procedure.
"Test Button" application
Use the Caption attribute to display text on the button to tell the user what the button does. In Figure 3.4, the test button example in the control sample application contains a CommandButton with a Caption property of ChangeSignal (this example works for Buttons.frm in the Controls.vbp sample application).
Note that S is the shortcut key for this button, represented by an underline. Inserting an (&) symbol into the text of the Caption attribute will make the characters following this symbol the shortcut key for the button (for example, Change&Signal).
When the user clicks the CommandButton, the code in the Click event process of the CommandButton will be executed. In this example, each time the button is clicked, it will change to a different traffic light icon.
For more information about the properties of CommandButton, see Chapter 7, "Using Visual Basic's Standard Controls."
Controls for displaying and entering text
Label and TextBox controls are used to display and enter text. Use a Label when the application displays text in a form, and a TextBox when allowing the user to enter text. The text in Labels is read-only text, while the text in TextBox is editable text.
Text that can be edited by the user, such as a sequential entry TextBox
field or a password box
Text that can only be displayed, such as identifying a Label in a form
fields or display instructions to the user
Labels and TextBoxes are discussed in the following sections:
Display text with Labels (vbconUsingLabels) Basic knowledge of using Label controls.
Basics of working with TextBox using TextBox (vbconWorkingWithTextBox).
Display text with Label
The text displayed by the Label control cannot be modified directly by the user. Controls such as TextBox and ScrollBar that do not have their own Caption property can be identified with Label. The text actually displayed in the Label is controlled by the Caption property, which can be set in the "Properties" window at design time or assigned with code at runtime.
By default, the title is the only visible part of the Label control. However, if you set the BorderStyle property to 1 (which can be done at design time), then the Label will have a border and look like a TextBox. You can also change the appearance of the Label by setting the Label's BackColor, BackStyle, ForeColor, and Font properties.
Change Label size to fit its content
You can specify the title of a single-line Label in the "Properties" window during design. But what if you want to enter a longer title or a title that may change at runtime? Label provides two properties: AutoSize and WordWrap to help you change the size of the control to fit a longer or shorter title.
The AutoSize property determines whether the control automatically changes size to fit its content. If this property is set to True, the Label will change horizontally according to its content, as shown in Figure 3.5.
The WordWrap property causes the Label to change vertically according to its content while keeping its width unchanged, as shown in Figure 3.6. For a working version of this example, see WordWrap.frm in the application example Controls.vbp.
Note that if you run the AutoSize example in Controls.vbp, you will find that both checkboxes must be selected in order to use WordWrap at the same time. This is because, in order for the Label's WordWrap property to work, you must set AutoSize to True. The Label's width will only increase if the width of a single word exceeds the current width of the control.
For more information about the Label control properties, see Chapter 7, "Using Visual Basic's Standard Controls."
Using TextBoxes
TextBox is a general-purpose control that can input text by the user or display text. Unless the Locked property of the TextBox is set to True, you cannot use the TextBox to display text that you do not want the user to change.
The actual text displayed in the TextBox is controlled by the Text property. The Text property can be set in three ways: in the Properties window at design time, through code at runtime, or by user input at runtime. The current contents of the TextBox can be retrieved at runtime by reading the Text property.
Multiline TextBox and WordWrap
TextBox only displays a single line of text by default and does not display ScrollBar. If the text is longer than the available space, only part of the text will be displayed. By setting the MultiLine and ScrollBars properties (which can only be set when designing the program), you can change the appearance and behavior of the TextBox.
Be careful not to confuse the ScrollBars property with the ScrollBar control. The ScrollBar control does not belong to the TextBox and has its own set of properties.
Setting the MultiLine property to True allows the TextBox to accept or display multiple lines of text at runtime. As long as there is no horizontal ScrollBar, the text in the multi-line TextBox will automatically wrap according to the word. The default value of the ScrollBars property is set to 0 (None). Automatic word wrapping saves users the trouble of inserting line breaks at the end of lines. When a line of text exceeds the display length, TextBox automatically folds the text to the next line for display.
At design time, line breaks cannot be entered in the Properties window. During the process, a line breakpoint can be generated by inserting a carriage return followed by a newline character (ANSI characters 13 and 0). You can also use the constant vbCrLf to insert a carriage return and line feed combination. For example, the following event procedure is an example of placing two lines of text into a multi-line TextBox (Text1) when the form is loaded.
SubForm_Load()
Text1.Text=Herearetwolines_
&vbCrLf&inatextbox
EndSub
Using text from TextBox
Using the SelStart, SelLength, and SelText properties of TextBox, you can control the insertion point and selection behavior of TextBox. These properties are only available at runtime.
When a TextBox first receives focus, the TextBox's default insertion point and cursor position are at the far left of the text. Users can move them with the keyboard and mouse. When the TextBox loses focus and then regains focus, the insertion point position is the same as the last position set by the user.
In some cases, it may be inconsistent with user settings. For example: in a word processing application, the user will want new characters to appear after existing text; in a data entry application, the user will want his input to replace the original entry. Using the SelStart and SelLength properties, the user can change the behavior of the TextBox as needed.
The SelStart property is a number indicating the insertion point within the text string, where 0 represents the leftmost position. If the SelStart property value is greater than or equal to the number of characters in the text, the insertion point will be placed after the last character, as shown in Figure 3.7. For a working version of this example, see Text.frm in the application example Controls.vbp. The SelLength property is a value that sets the width of the insertion point. Setting SelLength to a value greater than 0 will select and highlight SelLength characters starting from the current insertion point. Figure 3.8 shows the performance of the selection.
If a piece of text is selected, the text typed by the user will replace the selected text. In some cases, you can also use the Paste command to replace the original text with new text. The SelText property is a string of text that can be assigned a value at runtime to replace the currently selected text. If there is no selected text, SelText will insert text at the current insertion point.
For more information about the properties of the TextBox control, see Chapter 7, "Using Visual Basic's Standard Controls."
For more information about the properties of the TextBox control, see Chapter 7, "Using Visual Basic's Standard Controls."
->