HOWTO: Create a Private Data Collection for an MDI Child Form

ID: Q154666


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 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 Visual Basic. Form1 is created by default.


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

    1. From the Project Menu, choose Add MDI Form. In Visual Basic 4.0, choose MDI Form from the Insert menu.


    2. Add a PictureBox control to the MDIForm1 form.


    3. Add a Command button to the PictureBox control.


    4. 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 


    5. Make the MDIForm1 form the startup form by completing the following steps:

      1. From the Project menu, choose Project1 Properties. In Visual Basic 4.0, choose Options from the Tools menu, and select the Project tab.


      2. Select MDIForm1 from the Startup Object dropdown. In Visual Basic 4.0, select MDIForm1 from the Startup Form dropdown. In the Startup Form combo box, click MDIForm1.


      3. Click OK to close the dialog box.






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

    1. Choose Add Form from the Project Menu. In Visual Basic 4.0, choose Form from the Insert menu.


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


    3. 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.

      1. Change the MDIChild property to True.


      2. Add a command button.


      3. 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:

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


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


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


      4. For each Form2 form, enter a different first and last name in the text boxes and click the Set Property button.


      5. 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.






Additional query words: kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbNoKeyWord

Keywords : kbGrpVB
Version :
Platform : NT WINDOWS
Issue type : kbhowto


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.