Click the button to perform the action
The easiest way to interact with an application is to provide a button to the user. You can create your own buttons by using the CommandButton control provided by Visual Basic, or by using an Image control that contains graphics, such as an icon.
Using CommandButton
Most Visual Basic applications have CommandButton that allow users to perform actions with a simple tapping button. When a user selects a button, it not only performs the action, but also makes the button appear to be pressed and released. Whenever a user clicks a button, the Click event procedure is invoked. Writes code to the Click event procedure to perform the action you want to perform.
There are several ways to select CommandButton when a program is executing:
Click the button with the mouse.
Press the TAB key to move the focus to the appropriate button, and then press the space key or ENTER key to select the button. See "Focus Overview" later in this chapter.
Press CommandButton's shortcut key (ALT + underlined character).
The Value property set CommandButton in code is True. Cmdclose.value = True
Call the CommandButton Click event in your code. Cmdclose_click
If the CommandButton is the default command button for a form, even if you move the focus to a control other than CommandButton, pressing ENTER also selects the button. At design time, specify the default CommandButton for a form by setting the default property of a button to True.
If the CommandButton is the default Cancel button for the form, even if you move the focus to another control, you can select the button by pressing the ESC key. At design time, specify that it is the default Cancel button for the form by setting the Cancel property of a button to True. All of these actions cause Visual Basic to invoke the Click event procedure.
The test button application
You can use the Caption property to display text on a button that tells the user what the button is capable of. In Figure 3.4, the test button example in the Control sample application contains a CommandButton with the Caption property "Change Signal" (This example works as BUTTONS.FRM in the sample application Controls.vbp).
Note that S is the shortcut key for this button, which is indicated by an underscore. Inserting a (&) symbol in the body of the Caption property causes the character following the symbol to become the shortcut key for the button (for example, change &signal).
When the user clicks CommandButton, the code in the CommandButton click event procedure is executed. In this example, each click of the button will transform a different traffic light icon.
For more information about the properties of CommandButton, see chapter Seventh "Standard controls with visual Basic."
Controls for displaying and entering text
Label and TextBox controls are used to display and enter text. Use Label when the application displays text in a form, allowing the user to enter text with a TextBox. The text in the Labels is read-only and the text in the 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 recognizing a Label on a form
field or display instructions to the user
Labels and textboxes are discussed in the following sections:
Using label display text (vbconusinglabels) uses the basics of the label control.
Use the TextBox (Vbconworkingwithtextbox) to use the basics of the textbox.
Display text with Label
The text user that the Label control displays cannot be modified directly. such as TextBox and ScrollBar controls that do not have their own Caption properties can be identified by a Label. The text that is actually displayed in the Label is controlled by the Caption property, which can be set in the Properties window at design time or assigned in code at run time.
By default, the caption is the only visible part of the Label control. However, if you set the BorderStyle property to 1 (which you can do at design time), the Label has a border that looks like a TextBox. You can also change the appearance of a label by setting the BackColor, BackStyle, ForeColor, and Font properties of the label.
Change the Label size to fit its contents
You can specify the caption of a single line Label in the Properties window at design time. But what if you want to enter a longer or possibly changing title at run time? The Label provides two properties: AutoSize and WordWrap, which help you change the size of the control to fit a longer or shorter title.
The AutoSize property determines whether the control automatically changes its size to fit its contents. If the property is set to True,label, it will vary horizontally according to its contents, as shown in Figure 3.5.
The WordWrap property causes the Label to change vertically according to its contents, while keeping its width unchanged, as shown in Figure 3.6. For the working version of this example, see Wordwrap.frm in the Application sample Controls.vbp.
Note If you run the aotosize example in Controls.vbp, you will find that you must have two check boxes selected in order to use WordWrap at the same time. This is because, to make the WordWrap property of a Label work, you must set AutoSize to True. The width of a Label increases only if the width of one word exceeds the current width of the control.
For more information about Label control properties, see Chapter Seventh, "Standard controls for using VisualBasic."
The
Use text Boxes
TextBox is a generic control that allows users to enter text or display text. You cannot use a textbox to display text that you do not want the user to change unless you set the TextBox's Locked property to True. The actual text displayed in the
TextBox is controlled by the Text property. The Text property can be set in three ways: at design time, in the Properties window, at run time by code, or at run time by user input. You can retrieve the current contents of a TextBox at run time by reading the Text property. The
Multiline text Box and Word Wrap
TextBox display only single-line text by default and do not display ScrollBar. If the text length exceeds the available space, only part of the text is displayed. You can change the appearance and behavior of the TextBox by setting the MultiLine and scrollbars two properties, which can be set only when you design the program.
Note that the ScrollBars property is not confused with the ScrollBar control, and the ScrollBar control does not belong to a TextBox and has its own set of properties. The
set the Multiline property to True to allow the TextBox to accept or display multiple lines of text at run time. Text in a multiline TextBox is automatically wrapped as long as there is no horizontal direction to ScrollBar. The default value for the ScrollBars property is set to 0 (None). Automatic word wrapping eliminates the user's trouble inserting line breaks at the end of a line. When a line of text has exceeded the length that it can display, the textbox automatically retraced the text to the next line.
at design time, you cannot enter line breaks in the Properties window. During the procedure, you can create a line breakpoint by inserting a carriage return plus a newline character (ANSI characters 13 and 0). You can also use a constant vbCrLf to insert a carriage return with a line feed character combination. For example, the following event procedure is an example of putting two lines of text into a multiline TextBox (TEXT1) when the form is loaded.
Sub Form_Load ()
Text1.Text = ' Here are two lines ' _
& vbCrLf & ' In a Text box '
End Sub
Working with text in a TextBox
Using the SelStart, SelLength and SelText properties of the textbox, you can control the insertion point and selection behavior of the TextBox. These properties can be used only at run time.
When a TextBox gets focus for the first time, the default insertion point and cursor position of the textbox are at the far left of the text. Users can move them with the keyboard and mouse. When the TextBox loses focus and then gets it, the insertion point position is the same as the user's last set position.
In some cases, it may not be consistent with user settings. For example, in a word processing application, the user would want the new character to be behind the existing text, and in the data entry application, the user would 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 that indicates the insertion point within the text string, where 0 represents the leftmost position. If the value of the SelStart property is greater than or equal to the number of characters in the text, the insertion point is placed after the last character, as shown in Figure 3.7. For the working version of this example, see Text.frm in the Application sample Controls.vbp. The SelLength property is a numeric value that sets the width of the insertion point. Setting the SelLength to a value greater than 0 selects and highlights SelLength characters starting at the current insertion point. Figure 3.8 shows the selected performance.
If a paragraph of text is selected, the text that the user types will replace the selected one. In some cases, you can also use the Paste command to replace the original text with the new text. The SelText property is a string of text that can be assigned to it at run time to replace the currently selected text. If no text is selected, SelText inserts the text at the current insertion point.
For more information about the properties of a TextBox control, see Chapter Seventh "Standard controls with visual Basic."
For more information about the properties of a TextBox control, see Chapter Seventh "Standard controls with visual Basic."