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.
- 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.
- 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.
- 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
- 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
- Save the project.
- 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.