HOW TO: Call the Save Method of an MDI Child Form from the MDI Form Toolbar Menu by Using Visual Basic 6.0 (818367)



The information in this article applies to:

  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0

SUMMARY

This step-by-step article describes how to call the Save method in a multiple document interface (MDI) child form when you click the Toolbar button on the MDI Form.

back to the top

Requirements

The following lists the recommended hardware, software, network infrastructure, and service packs that you must have:

Microsoft Visual Basic 6.0

This article assumes that you are familiar with the following:
  • Microsoft Visual Basic 6.0 IDE
  • Microsoft Visual Basic 6.0 programming
back to the top

Call the Save Method in an MDI Child Form

  1. Start Visual Basic 6.0.
  2. On the File menu, click New Project. In the New Project dialog box, click Standard EXE, and then click OK.
  3. In Solution Explorer, right-click Form1, and then click Remove Form1.
  4. On the Project menu, click Add MDI Form.
  5. In the Add MDI Form dialog box, click MDI Form, and then click Open.
  6. Set the Name property of the MDI Form to frmMDI, and then set the WindowState property to Maximized.
  7. On the Project menu, click Add Form. In the Add Form dialog box, click Form, and then click Open.

    By default, the Form1 form is created
  8. Set the MDIChild property of the Form1 form to True, and then set the Borderstyle property of the Form1 form to Fixed Dialog.
  9. Add a command button and a text box to the Form. Set the Name property of Command Button to cmdSave, and then set the Caption property to Save.
  10. To view the code editor, double-click the Save command button. Change the Click event scope of the cmdSave event handler from Private to Public.
  11. Repeat step 5 through step 8 two times. This adds MDI child form Form2 and MDI child form Form3.
  12. Add the following code to the Click event of the Save command button of the MDI child form Form1:
    MsgBox " Save event of MDI Child Form: Child1"
  13. Add the following code to the Click event of the Save command button of the MDI child form Form2:
    MsgBox " Save event of MDI Child Form: Child2"
  14. Add the following code to the Click event of the Save command button of the MDI child form Form3:
    MsgBox " Save event of MDI Child Form: Child3"
  15. On the Project menu, click Components. Click Microsoft Windows Common Controls 6.0, and then click OK.
  16. Double-click the Toolbar control in the toolbox. Right-click Toolbar, and then click Properties.
  17. Click the Buttons tab in the Toolbar Properties dialog box, and then click Insert Button to add a Toolbar button. Set the Caption property of the Toolbar button to Save, and then set the Key property to Save.
  18. Click OK.
  19. To view the code editor, double-click the Toolbar to open in Code view.
  20. Add the following code to the Click event of the Toolbar:
       On Error GoTo NoActiveForm
       Select Case Button.Key
          Case "save"
             Me.ActiveForm.cmdSave_Click
          End Select
       Exit Sub
    
    NoActiveForm:
       MsgBox Err.Description
    
  21. Add the following code to the Load event of the MDI Form frmMDI to show all the MDI child forms when the application starts.
       Form1.Show
       Form2.Show
       Form3.Show
    
  22. On the Project menu, click Project Properties. In the Startup Object combo box on the General tab, click frmMDI as the startup object.
back to the top

Verify That It Works

  1. On the Run menu, click Start.

    The MDI application with three MDI child forms appears. Each MDI child form has a text box and a Save command button.
  2. Click Save on the Toolbar while the Form Child3 is active. A message box displays the following message:

    Save event of MDI child form: Child3
  3. Randomly make other MDI child forms active, and then click Save on the toolbar to see if the correct child form Save method is called whenever you click Save on the toolbar.
back to the top

REFERENCES

For more information about how to work with MDI forms and child forms and about how to add buttons to a toolbar, visit the following Microsoft Web sites:

http://msdn.microsoft.com/library/en-us/vbcon98/html/vbconworkingwithmdiformschildforms.asp

http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskaddingbuttonstotoolbar.asp?

back to the top

Modification Type:MinorLast Reviewed:8/19/2005
Keywords:kbui kbControl kbMDI kbToolbar kbHOWTOmaster KB818367 kbAudDeveloper