How to Create a Private Data Collection for an MDI Child Form

Last reviewed: January 10, 1997
Article ID: Q154666
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

A form can contain public and private properties similar to the properties found in the Properties window of a form or component. You can associate data with individual forms by creating a property procedure. Property procedures allow individual forms to hold unique data sets.

This article demonstrates how to create property procedures with an MDI application containing a child form template. As a result of using this technique, each instance of the child form can then contain a unique data set.

MORE INFORMATION

Steps to Create Sample Program

  1. Start the 16-bit or 32-bit edition of Visual Basic 4.0, or if it is already running, click New Project on the File menu.

  2. Add an MDI form to the project by completing the following steps:

    a. From the Insert menu, click MDI Form.

    b. Add a PictureBox control to the MDIForm1 form.

    c. Add a Command button to the PictureBox control.

    d. Copy the following code to the Code window of the MDIForm1 form:

             Private Sub MDIForm_Load()
                Command1.Caption = "New Child Form"
             End Sub
    
             Private Sub Command1_Click()
                Dim F As New Form1
                F.Caption = "Child " & Forms.Count
                F.Show
             End Sub
    
       e. Make the MDIForm1 form the startup form by completing the following
          steps:
    
          1. From the Tools menu, click Options.
    
          2. Click the Project Tab.
    
          3. In the Startup Form combo box, click MDIForm1.
    
          4. Click OK to close the Options dialog box.
    
    

  3. Add another form to the project by completing the following steps:

    a. On the Insert menu, click Form to insert another form into the

          project.
    

    b. Add two command buttons and two text box controls to the Form2 form.

    c. Copy the following code to the Code window of the Form2 form:

             Private Sub Form_Load()
                Text1.Text = ""
                Text2.Text = ""
                Command1.Caption = "Set Property"
                Command2.Caption = "Get Property"
             End Sub
    
             Private Sub Command1_Click()
                With MDIForm1.ActiveForm
                   .FirstName = Text1.Text
                   .LastName = Text2.Text
                End With
             End Sub
    
             Private Sub Command2_Click()
                With MDIForm1.ActiveForm
                   Text1.Text = .FirstName
                   Text2.Text = .LastName
                End With
             End Sub
    
    

  4. Make the following changes to the Form1 form.

    a. Change the MDIChild property to True.

    b. Add a command button.

    c. Copy the following code to the Code window of the Form1 form:

             Private TempFirstName As String
             Private TempLastName As String
    
             Private Sub Form_Load()
                Command1.Caption = "Show Dialog Box"
             End Sub
    
             Private Sub Command1_Click()
                Form2.Show
             End Sub
    
             Public Property Get FirstName()
                FirstName = TempFirstName
             End Property
    
             Public Property Let FirstName(vNewValue)
                TempFirstName = vNewValue
             End Property
    
             Public Property Get LastName()
                LastName = TempLastName
             End Property
    
             Public Property Let LastName(vNewValue)
                TempLastName = vNewValue
             End Property
    
    

  5. Save the project.

  6. Run the sample program by completing the following steps:

    a. On the Run menu, click Start or press the F5 key to start the

          program.
    

    b. Create two child forms by clicking the New Child Form button twice.

    c. For each child form, click the Show Dialog button to display a Form2

          form.
    

    d. For each Form2 form, enter a different first and last name in the

          text boxes and click the Set Property button.
    

    e. From each child form, click the Show Dialog button again to display

          the Form2 form created from the child form. Click the Get Property
          button. Each Form2 form should contain its own first and last name
          data items in the text boxes.
    


KBCategory: kbprg kbHowTo
KBSubcategory: PrgOther
Additional reference words: 4.00 kbdsd vb4win vb4all



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.