button controls generally accept that a mouse click event is used to start, break, or end a process (equivalent to a previous version of Visual Basic CommandButton control). When you click the button control, the procedure that has been written to the Click event is invoked. Button controls are used in most Visual Basic applications, and users can click buttons to perform actions. When clicked, the button does not only perform the appropriate action, but also appears to be pressed and released. The icon for the button control in the Toolbox is as shown in the figure:
1. Add a button to a form
More than one button is likely to be used in an application. Just like drawing a button on another container control, drag the button control from the toolbox directly onto the form. You can resize the button by using the mouse, or by setting location (coordinates that determine the position of the control relative to the top left of the form) and size (size, the first parameter represents the width, and the second parameter represents the height) property to adjust.
2. Set button display text
You can change the text displayed on the button by using the Text property. At design time, you can set this property in the Properties window of the control. When you set the Text property at design time, the button text is updated dynamically. The Text property contains a maximum of 255 characters. If the caption exceeds the width of the command button, it will be folded to the next line. However, if the control cannot hold its full length, the caption is clipped. You can change the font displayed on a command button by setting the Font property.
3. Create keyboard shortcuts
You can create an access key shortcut to a button by using the Text property. To do this, simply add a hyphen (&) before the letter that is the access key. For example, to create an access key for the heading "OK", add a hyphen to the letter "O" and get "&ok". At run time, the letter "O" is underlined and the Alt+o key is pressed to perform the action that the Click Button program performs.
Note: If you do not create an access key and then include a hyphen in the title, you should add two hyphens (&&). In this way, only one hyphen is displayed in the title.
4. Select button
At run time, you can use the mouse or keyboard to select a button by using the following methods:
(1) Click the button with the mouse;
(2) Press the TAB key to transfer focus to the button, and then press ENTER to select the button;
(3) Press the access key of the button ([Alt + underlined letter]).
5.Click Event
Clicking a button at run time triggers the Click event of the button and executes the code in the process of writing the Click event, and the process of clicking the button also generates events such as MouseMove, MouseLeave, MouseDown, and MouseUp. If you want to attach an event procedure to these related events, you should ensure that the operation does not collide. Unlike controls, these event procedures occur in a different order. Click events for button controls occur in the order of:
Mousemove→mousedown→click→mouseup→mouseleave
Note: If a user tries to double-click a button control, each click is handled separately, or the button control does not support double-clicking the event.
6. Enhance the visual effect of the button
button controls like check boxes and option buttons, you can use the Image property to set the icon on the button control to enhance the visual effect, and then set the properties of the icon (picture): ImageAlign Displays the location of the icon (picture). By setting ImageIndex (the index of the picture in the picture box) and the ImageList (picture frame), you can do the following, such as adding an icon or bitmap to the button, or displaying a different image when you click or disable the control, and so on.