VB toolbar, dialog box (i)

Source: Internet
Author: User
Tags exit contains continue reference requires

You can use toolbars to further enhance the application's menu interface. The toolbar contains a toolbar button that provides quick access to the most commonly used commands in your application. For example, a toolbar in Visual basic contains toolbarbutton that perform common commands, such as open (open an existing project), save (store the current project), and so on.

Create a toolbar
Toolbars, also known as clockwork or control bars, have become standard features of many windows-based applications. The toolbar provides quick access to the most commonly used menu commands in your application. Using the toolbar control to create a toolbar is easy and convenient, and is available in both Professional and Enterprise editions of Visual Basic. If you are using the Visual Basic Learning version, you can create toolbars by hand, as described in "Coordinating menus and toolbars Appearance" later in this chapter.
The following example shows the procedure for creating a toolbar for an MDI application, and the process of creating a toolbar on a standard form is essentially the same.
To create a toolbar manually, follow these steps:
1. Place a picture box on the MDI form. The width of the picture frame stretches automatically until the MDI form workspace fills up. The workspace is the area within the form's border, excluding the title bar, menu bar, or all toolbars, status bars, or scroll bars that may be on the form.
Note only those controls that directly support the Align property are placed on the MDI form (the picture box is the only standard control that supports this property).
2. In the picture box, you can place any control that you want to display on the toolbar. Typically, you use a CommandButton or image control to create a toolbar button. Figure 6.16 shows a toolbar that contains an image control.
To add a control to the picture box, click the Control button in the toolbar, and then draw it in the picture box.
Note When a picture box is included in an MDI form, the inner area of the MDI form does not include a picture frame. For example, the Scaleheigh property of an MDI form returns the inner height of the MDI form, which does not include the height of the picture frame.

3. Set design-time properties.
One advantage of using toolbars is that you can display an image of the command icon. The image control is a good choice as a toolbar button because it can be used to display a bitmap. Sets its Picture property to display a bitmap at design time, so that when the button is clicked, it can provide visible information about the execution of the command. You can also use ToolTips by setting the button's ToolTipText property so that when the user holds the mouse pointer over a button, the name of the toolbar button can be displayed.
4. Write code
Because toolbars are frequently used to provide quick access to other commands, other procedures, such as corresponding menu commands, are called from each button's Click event for most of the time.
Tip You can use a control that is not visible at run time, such as a timer control, by using an MDI form that does not display a toolbar. To do this, place a picture box on the MDI form, place the control in the picture box, and then set the Visible property of the picture box to false.

Code to write a toolbar
The toolbar is used to provide a quick way to access certain application commands. For example, the first button on the toolbar in Figure 6.16 is the shortcut key for the new File command. You can now request the creation of a new file in three places in the Mdinotepad sample application.
1. On the MDI form (on the MDI form, on the File menu, the new command).
2. On the subform (on the Subform File menu, new command).
3. On the toolbar ("New File" button).
Instead of repeating the code three times, you might as well remove the original code from the Mnufilenew_click event of the subform and put it in a common procedure on the subform. You can call this procedure from any of the above event procedures. Here's an example:
' This routine is in the public process.
Public Sub FileNew ()
Dim Frmnewpad as New frmnotepad
Frmnewpad.show
End Sub

' Choose New on the File menu of the subform.
Private Sub Mnuchildfilenew_click ()
FileNew
End Sub

' Choose New on the File menu of the MDI form.
Private Sub Mnumdifilenew_click ()
Frmnotepad.filenew
End Sub

' Click the New File button on the toolbar.
Private Sub Btnfilenew_click ()
Frmnotepad.filenew
End Sub

Coordinating the appearance of menus and toolbars
When an object provided by another application is activated in a form, there are many ways to make the object's menus and toolbars appear in the container window, however, you need to specify how they will appear. This process is called user interface coordination because Visual Basic and already linked or embedded objects must reconcile space in the container form.

Controlling the appearance of menus
You can determine whether the menu of a linked or embedded object appears in the container form by setting the NegotiateMenus property of the form. If the NegotiateMenus property of the subform is set to True (the default) and the container has a defined menu bar, then when the object is activated, its menu is placed in the container's menu bar. If the container does not have a menu bar, or if the NegotiateMenus property is set to False, the object's menu will not appear when the object is activated.
Note the NegotiateMenus property does not apply to MDI forms.

Control the appearance of toolbars
The NegotiateToolbars property of an MDI form determines whether the toolbar of a linked or embedded object is an unfixed palette or is placed on a parent form. This performance does not require the toolbar to appear on the MDI parent form. If the NegotiateToolbars property of the MDI form is set to true, the object's toolbar appears on the MDI parent form. If NegotiateToolbars is set to False, the object's toolbar is an unfixed palette.
Note the NegotiateToolbars property is used only for MDI forms.
If the MDI form contains toolbars, it is usually included in the PictureBox control of the parent form. The Negotiate property of the picture box determines whether the container's toolbar will continue to be displayed or replaced by the object's toolbar when activated. If negotiate is set to true, the object's toolbar is displayed in addition to the container's toolbar. If negotiate is set to False, the object's toolbar replaces the container's toolbar.
Note that the coordination of menus and toolbars occurs only on the pluggable objects that support field activation. For more information about onsite activation, see Chapter Tenth, "Programming with parts." Use the following procedure to see how these three properties interact.
To perform the coordination of menus and toolbars, follow these steps:
1. Add a toolbar to the MDI form. This is described in the "Creating toolbars" earlier in this chapter.
2. Place a pluggable object on the subform.
3. Set the NegotiateMenus, NegotiateToolbars, and Negotiate properties.
4. Run the application, and then double-click the object.

dialog box

In windows-based applications, dialog boxes are used to:
Prompts the user to provide the data that the application needs to continue executing.
Displays information to the user.
For example, in Visual Basic, use the Open File dialog box to display an existing project. The "About" dialog box in VisualBasic is also an example of how to use a dialog box to display information. On the menu bar, click Help, and then select the About Visual Basic menu item to display the About dialog box.

The

Mode and Modeless dialog box
dialog box is not modal or modeless. modal dialog box, which must be closed (hidden or unloaded) before it can continue to operate on other parts of the application. For example, if a dialog box requires that you click OK or cancel before you can switch to another form or dialog box, it is modal. The
About dialog box in Visual Basic is modal. A dialog box that displays important messages should always be modal-that is, before proceeding, always ask the user to close the dialog box or respond to its message. The
Modeless dialog box allows you to transfer focus between a dialog box and another form without closing the dialog box. When the dialog box is being displayed, you can continue to work elsewhere in the current application. Modeless dialog boxes are rarely used. The Find dialog box on the Edit menu in Visual Basic is an instance of a modeless dialog box. Modeless dialog boxes are used to display frequently used commands and information.
to display the form as a modal dialog box,
1. Use the Show method with a style parameter value of vbmodal (a constant with a value of 1). For example,
' displays the Frmabout as a modal dialog box.
Frmabout.show vbmodal
to display the form as a modeless dialog box,
2. Use the Show method without the style parameter.
For example:
' displays frmabout as modeless dialog box.
Frmabout.show
Note If the form is displayed as a modal dialog box, the code after the Show method can only be executed when the dialog box is closed. However, when the form is displayed as a modeless dialog box, the code that follows the Show method executes immediately after the form is displayed. The
Show method has an optional parameter, owner, that you can use to specify a parent-child relationship for the form. You can pass a form name to this parameter, making the form the owner of the new form. To display a form as a subform for another form,
The Show method has two parameters: style and owner.
For example:
' displays frmabout as a modeless subform for frmmain.
Frmabout.show vbmodeless, Frmmain
uses the owner parameter in the Show method to ensure that the dialog box is minimized when its parent form is minimized, or it is unloaded when its parent form is closed.

Use of predefined dialog boxes
The easiest way to add a dialog box to your application is to use a predefined dialog box, because you don't have to consider problems with designing, loading, or displaying dialog boxes. However, controls are limited in their appearance. A predefined dialog box is always modal.
The following table lists the functions that can be used when adding predefined dialog boxes in a Visual basic application.

Use the input box to prompt for input
Apply the InputBox function request to provide the data. This function displays a modal dialog box that requires data to be entered. The text input box shown in Figure 6.17 prompts you for the name of the file you want to open.

The following code shows the input box shown in Figure 6.17.
FileName = InputBox ("Enter file to open:", "File Open")
Note that when you use the InputBox function, there is very limited control over the parts of the dialog box. You can change only the text in the title bar, the command prompt displayed to the user, the location of the dialog box on the screen, and whether it displays a Help button.
For more information, see the "InputBox function" in the language reference.

Displaying information in a message dialog box
You can use the MsgBox function to get "yes" or "no" responses and display short messages, such as errors, warnings, or expectations in a dialog box. After reading these messages, you can select a button to close the dialog box.
If the file cannot be opened, the application named Text Editor will display the message dialog box shown in Figure 6.18.

The following code shows the message box as shown in Figure 6.18:
MsgBox "Error encountered while trying to open file, _
Please retry. ", vbexclamation," Text Editor "
Note that the so-called mode, can be limited to the application, can also be limited to the system. If the mode of the message box is limited to the application (default), you cannot switch to other parts of the application until the dialog box is gone, but you can switch to another application. The system's modal message box does not allow switching to another application before the message box disappears.
For more information, see the "MsgBox function" in the language reference.

Use a form as a custom dialog box
A custom dialog box is a form that the user creates that contains controls--These include command buttons, selection buttons, and text boxes--they can receive information for the application. Customize the appearance of a form by setting property values. You can also write code that displays a dialog box at run time.
To create a custom dialog box, you can start with a new form or customize a ready-made dialog box. If you repeat too much, you can build a collection of dialog boxes that you can use in many applications.
To customize an existing dialog box, follow these steps:
1. Choose Add Form from the Project menu to add an existing form to the project.
2. Choose "FileName Save as" from the File menu and enter a new file name (this prevents changes to the existing version of the form).
3. Customize the appearance of the form as needed.
4. Customize the event procedure in the Code window.
To create a new dialog box, follow these steps:
1. Choose Add Form from the Project menu.
Or
Create a new form by clicking the Form button on the toolbar.
2. Customize the appearance of the form if necessary.
3. Customize the event procedure in the Code window.
There's a lot of freedom to define the appearance of a custom dialog box. It can be fixed or movable, modal, or modeless. It can contain different types of controls; However, the dialog box usually does not include a menu bar, window scroll bar, minimized and maximized buttons, status bar, or a variable-size border. The remaining sections of this topic discuss creating typical types of dialog box methods.

Add title
A dialog box should always have a title that identifies it. To create a caption, set the form's Caption property to the text string that will be displayed in the title bar. Typically, this step is done at design time using the Properties window, but you can also use code to complete this step. For example:
Frmabout.caption = "about"
Tip If you want to completely remove this title bar, you can set the form's ControlBox, Minbutton, and Maxbutton to False, set BorderStyle to a dimension immutable setting (0, 1, or 3), and set the Caption to an empty string ("").

To set properties for a standard dialog box
In general, when a user responds to a dialog box, it first provides the information and then closes the dialog box with the OK or Cancel command button. Because the dialog box is temporary, the user usually does not need to move, resize, maximize, or minimize the operation. The result is that the variable dimension border Type, the Control menu box, the Maximize button, and the Minimize button that appear with the new form are not needed in most dialog boxes.
You can delete these items by setting the BorderStyle, ControlBox, Maxbutton, and Minbutton properties. For example, the About dialog box might use the following property settings.

Remember that if you delete the Control menu box (ControlBox = False), you must provide the user with an alternative way to exit the dialog box. This is usually accomplished by adding the OK, cancel, or exit command buttons in the dialog box and adding code to the Click button event that hides or unloads the dialog box.

Add and Drop command buttons
The modal dialog box must contain at least one command button that exits the dialog box. Typically, you use two command buttons: One button starts the action, and the other button closes the dialog box without making any changes. The typical state is that the Caption property of these two buttons is set to "OK" and "Cancel." In this scenario, the OK command button's default property is set to True, and the Cancel command button's Cancel property is set to true. Although "OK" and "Cancel" are the most commonly used buttons, other combinations of button headings are also available.

dialog boxes that display messages typically use a Label control to display an error message or a command prompt, and to perform an action with one to two command buttons. For example, you might assign an error message or a command prompt to the identity's Caption property, and assign "yes" and "no" to the Caption property of the two command button control. When the user selects Yes, an action occurs and another action occurs when no is selected.
The command buttons for this type of dialog box are usually placed at the bottom or right of the dialog box, while the top or left buttons are the default buttons, as shown in Figure 6.19.

Set default, Cancel, and focus
The command button control provides the following properties:
Default
Cancel
TabIndex
Tabstopdefault
The button is the button that is selected when the user presses the ENTER key. On a form, only one command button's default property can be set to True. The Click event of the default command button is invoked when the ENTER key is pressed. This feature works with editing controls, such as a TextBox. For example, you can type the data in the text box, and then press ENTER to produce the click event instead of selecting the OK button.
The Cancel button is the button that is selected when the ESC key is pressed. On a form, only one command button's Cancel property can be set to True. Press the ESC key to invoke the Click event of the Cancel command button. The Cancel button can also be the default command button. To specify the Cancel button for the dialog box, set the Cancel property of the command button to True.
Tip Generally speaking, a button that represents the most reliable or safest operation should be the default button. For example, in the Text Replacement dialog box, cancel should be the default button instead of replace All.
You can also specify the button that has the focus when the dialog is displayed. When the form is displayed, the control with the lowest TabIndex setting receives the focus. Pressing the ENTER key invokes the default command button or the Click event of the command button with focus. To give the focus to a command button when the form is displayed, the command button is set to TabIndex 0, and its TabStop property is True. You can also use the SetFocus method to give focus to the specified control when the form is displayed.
For more information, see the TabIndex property and TabStop properties of the language reference.

Invalidates the control on the dialog box
Sometimes you need to invalidate the controls because their actions do not apply in the current context. For example, when the Find dialog box for Visual Basic appears for the first time, the Find Next button should not work, as shown in Figure 6.20. Set the Enabled property of the control on the dialog to False, and you can invalidate it.
To invalidate the controls on the dialog box,
1. Set the Enabled property of each control to False. For example:
cmdfindnext.enabled = False
cmdreplace.enabled = False

Display a custom dialog box
Displays the dialog box in the same way that other forms are displayed in your application. When the application is running, the startup form is automatically mounted. You want the second form or dialog box to appear in your application, and you want to implement it by loading and displaying its code. Similarly, if you want a form or dialog box to disappear, write the code that unloads or hides it.
The following code displays the About dialog box when the user selects the About menu item in the Help menu.
Private Sub Mnuhelpabout_click ()
' This shows the dialog box as modal using the style = VBModal Show method.
Frmabout.show vbmodal
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.