VB user Interface (ii)

Source: Internet
Author: User
Tags exit integer reference

MDI Notepad Application
The MDI NotePad sample application is a simple text editor similar to the NotePad application contained in MicrosoftWindows. However, the MDI NotePad application uses a multiple-document interface (MDI). At run time, the application creates a new instance of the subform if the user needs a new document (executed with the new command on the File menu of the application). This allows you to create as many subforms or documents as you want.
To create a document-centric application in Visual Basic, you need at least two forms: an MDI form and a subform. At design time, you should create an MDI form to accommodate the application, and then create a subform as a template for the application's documentation.
To create your own MDI NotePad application, follow these steps:
1. From the File menu, select the New Project command.
2. From the Project menu, select the Add MDI Form command to create the container form. The project should now contain an MDI form (MDIFORM1) and a standard form (FORM1).
3. Create a text box (TEXT1) on the Form1.
4. Set properties for two forms and text boxes in the following ways.

6. Add the following code during the Mnufilenew_click process:
Private Sub Mnufilenew_click ()
' Create a new instance of the form Form1 named Newdoc.
Dim Newdoc as New Form1
' Show this new form.
Newdoc.show
End Sub
This procedure creates and displays a new instance (or a copy) of the Form1 named Newdoc. Whenever you choose the new command from the File menu, a copy (instance) identical to the Form1 is created, containing all the controls and code that FORM1 contains.
7. Add the following code to the Form_resize procedure for the Form1 form:
Private Sub form_resize ()
' Expands the text box to place the current subform.
Text1.height = ScaleHeight
Text1.width = ScaleWidth
End Sub
The code for the Form_resize event procedure, like all code in Form1, can be shared for each instance of Form1. When you display several copies of a form, each form recognizes its own events. When an event occurs, the code for the event procedure is invoked. Because the same code is shared for each instance, the form on which the code is invoked is referenced, especially if each instance has the same name (FORM1). This issue will be discussed in the section "Working with MDI forms and their subforms" later in this chapter. 8. Press the F5 key to run the application.
Tip In addition to what is mentioned in this chapter, the MDINOTE.VBP sample application also contains many MDI tips. Taking some time to conduct a comprehensive study of the sample code will find these techniques. The Sdinote.vbp sample application is the implementation of the same application conversion to SDI style; compare these two examples to understand the difference between MDI and SDI techniques.

Working with MDI forms and their subforms
When an MDI application wants to open, save, and close several subforms in one session, it should be able to refer to the active form and maintain state information about the subform. This topic describes some of the coding techniques used to specify active subforms or controls, load and unload MDI forms and their subforms, and maintain state information for subforms.

Specify an Active subform or control
Sometimes you provide a command that operates on the control that has focus on the currently active subform. For example, suppose you copy selected text from a subform text box to the Clipboard. In the Mdinote.vbp sample application, the Click event for the copy item on the Edit menu will call Editcopyproc, which is the process of copying the selected text to the Clipboard.
Because the application can have many instances of the same subform, Editcopyproc needs to know which form to use. To specify this, use the ActiveForm property of the MDI form, which returns either the focus or the last activated subform.
Note that when you access the ActiveForm property, at least one MDI child form should be loaded or visible, or an error will be returned.
When you have several controls in a form, you also need to specify which control is active. Like the ActiveForm property, the ActiveControl property returns the control with the focus on the active subform. Below is an example of a replica routine that can be invoked from the subform menu, the MDI form menu, or a toolbar button.
Private Sub Editcopyproc ()
Copies the selected text to the Clipboard.
Clipboard.settext _
FrmMDI.ActiveForm.ActiveControl.SelText
End Sub
If you are writing code that is called by multiple form instances, It is a good idea to access a form's controls or properties without a form identifier. For example, use Text1.height to refer to the height of a text box on Form1 instead of using Form1.Text1.Height. In this way, the code always affects the current form. Another way to
Specify the current form in code is to use the Me keyword. Use the Me keyword to refer to the form that the current code is running. This keyword is useful when you need to pass the reference parameters of the current form instance to the procedure.
For more information about creating multiple form instances with the New keyword with the Dim statement, see the "variables, constants, and data Types Overview" in chapter Fifth, "Programming basics," and "dim statements" in the language reference. For information about creating multiple form instances with the New keyword with the Dim statement, see "Overview of variables, constants, and data types" in the programming basics, and "Dim statements" in the language reference.

Load an MDI form and its subform
When a subform is loaded, its parent form (the MDI form) is automatically loaded and displayed. When an MDI form is loaded, its subform does not automatically load.
In the MDI NotePad example, the subform is the default startup form, so both the subform and the MDI form are loaded when the program runs. If you change the startup form to frmMDI in an MDI NotePad application (on the General tab of the project properties), and then run the application, only the MDI form is loaded. The first subform is loaded when you choose the new command from the File menu.
The AutoShowChildren property can be used to load the MDI child windows of the hidden state so that they are hidden until they are displayed with the Show method. This allows you to update the details of the title, location, and menu before the subform becomes visible.
You cannot display an MDI child form or an MDI form as a modal form (with the Show method with vbmodal arguments). If you want to use a modal dialog box in an MDI application, you can use the form with the MDIChild property set to False.

Set the size and position of a subform
If the MDI child form has a variable-size border (that is, BorderStyle = 2), Microsoft Windows determines its initial height, width, and position when it mounts. A subform with a variable border size, with its initial size and position depending on the size of the MDI form, not the size of the subform at design time. When the border size of an MDI child form is immutable (that is, BorderStyle = 0,1 or 3), it is loaded with the Height and Width properties of the design time.
If you set AutoShowChildren to False, you can change its position before the MDI child form is loaded and set to the visible state.
For more information, see the "AutoShowChildren Properties" and "show methods" in the language reference.

Maintain status information for subforms
The user must have an opportunity to save information when they decide to exit the MDI application. For this to work, the application must be able to determine at any time whether the data in the subform has changed since the last time it was saved. The
implements this functionality by declaring a public variable in each subform. For example, you can declare a variable in the Declaration section of a subform: When the text in the
public booldirty as Boolean
Text1 changes one time, the change event for the subform text box sets the Booldirty to True. You can add this code to indicate that the contents of the TEXT1 have changed since the last save.
Private Sub text1_change ()
Booldirty = True
End Sub
Instead, the change event for the text box sets Booldirty to False each time the user saves the contents of the subform. To indicate that the contents of the Text1 no longer need to be saved. In the following code, suppose you have a menu command called Save (mnufilesave) and a procedure named FileSave that holds the contents of the text box:
Sub Mnufilesave_click ()
saves Text1 content.
FileSave
' sets the state variable.
Booldirty = False
End Sub

Uninstalling an MDI form with QueryUnload
The BOOLDIRTY flag is useful when a user decides to exit the application. This occurs when the user chooses close from the Control menu of the MDI form, or from the menu item provided, such as the Exit command on the File menu. If the user uses the MDI form's control menu to close the application, Visual Basic attempts to uninstall the MDI form.
When an MDI form is unloaded, the QueryUnload event is called first for the MDI form, and then for each open subform. If there is no code during these QueryUnload events, the Unload event is canceled, and each subform is unloaded, and finally, the MDI form is unloaded.
Because the QueryUnload event is invoked before the form is unloaded, you can give the user a chance to save the form before the form is unloaded. The following code uses the BOOLDIRTY flag to determine whether you want to remind users to save before the subform is unloaded. Note that you can access public form-level variable values anywhere in the project. This code assumes a process called FileSave, which saves the contents of the Text1 to a file.
Private Sub Mnufexit_click ()
' When a user selects the file Submission command in an MDI application, uninstall
' MDI form, which invokes the QueryUnload event for each open subform.
Unload frmMDI
End
End Sub
Private Sub form_queryunload (Cancel as Integer, _
UnloadMode as Integer)
If Booldirty Then
' Call the routine to ask the user and save the file if necessary.
FileSave
End If
End Sub
For more information, see "QueryUnload events" in the language reference.

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.