Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Arrays Of Forms

Now that you’ve written your MDI program, you suddenly have a lot of windows to manage. The user wants to open 20 documents at the same time—how can you keep track of all that? Wouldn’t it be nice if you could use arrays of forms in Visual Basic and just refer to each form with one single array index?

You can do that in Visual Basic (in fact, you can create arrays of many types of objects, excluding such objects that there can only be one of, like the application object, App). You create an array of forms just as you would create an array of any other kind of object; here, we’re creating an array of Form1 objects, because that’s the type of form we’ll use as MDI children in an MDI program:

Dim Forms(1 To 20) As Form1

If we declare this array, Forms(), as a form-level array in the MDI form, we can refer to that array in all procedures in the MDI form. For example, we might want to create and display a new MDI child form in a procedure named NewWindow_Click():

Private Sub NewWindow_Click()

End Sub

Next, we set up a static variable to hold the total number of MDI child forms, NumberForms, and increment that variable now that we’re adding a new form:

Private Sub NewWindow_Click()
    Static NumberForms
    NumberForms = NumberForms + 1
...
End Sub

Now, we create a new form and add it to the form array:

Private Sub NewWindow_Click()
    Static NumberForms
    NumberForms = NumberForms + 1
    Set Forms(NumberForms) = New Form1
...
End Sub

Throughout the rest of the program, now, we’re able to refer to the new form as a member of the form array; here, for example, we set its caption and show it, referring to it with an index value in the form array:

Private Sub NewWindow_Click()
    Static NumberForms
    NumberForms = NumberForms + 1
    Set Forms(NumberForms) = New Form1
    Forms(NumberForms).Caption = "Document" & Str(NumberForms)
    Forms(NumberForms).Show
End Sub

Coordinating Data Between MDI Child Forms (Document Views)

Your new word-processor program is almost done—just one more refinement to add. You want to allow the user to open multiple views into the same document. A view is just a window into a document, and if a document has multiple views open, the user can scroll around in different parts of the same document at the same time. You’ve been able to open the same document in several view windows now—but what if the user starts typing into one view? All the other views should also be updated with the new text as well. How do you keep all the open views of the same document coordinated?

We’ll see how this works now. In this example, the MDI child windows will be based on a form, Form1, in which we’ve placed a text box. The user can open as many MDI child windows as they like with the New item in the Window menu. When they type in one MDI child’s text box, however, we should mirror any such changes in the other MDI children’s text boxes as well. This is shown in Figure 4.20, where the text appears simultaneously in both MDI children while the user types into one.


Figure 4.20  Coordinating MDI children.

We start by adding a new module to the program with the Project[vbar]Add Module item so that we can set up a global array of forms, Forms, and an array index variable, NumberForms, in that module:

Public Forms(1 To 20) As Form1
Public NumberForms As Integer

Next, we add a Window menu to the MDI form. We also add new forms to that array of forms when the user creates such new forms by adding this code to the MDI form’s New item in the Window menu:

Private Sub NewWindow_Click()
    NumberForms = NumberForms + 1
    Set Forms(NumberForms) = New Form1
    Forms(NumberForms).Caption = "Document" & Str(NumberForms)
    Forms(NumberForms).Show
End Sub

Now the Forms array holds the MDI children in our program.

When the user types text into the text box displayed in an MDI child, we want to update all the other MDI children as well, making them display the same text. When you type into a text box, a Change event occurs, and we’ll add code to that event’s handler function to update all the other MDI children:

Private Sub Text1_Change()

End Sub

Here, we store the text in the just-changed text box and, in this simple example, just loop over all MDI children, updating them to match the changed text box:

Private Sub Text1_Change()
    Dim Text As String
    Text = Text1.Text
    For intLoopIndex = 1 To NumberForms
        Forms(intLoopIndex).Text1.Text = Text
    Next intLoopIndex
End Sub

Now when you change the text in one child, the text in all children is updated. In this way, we can support multiple views into the same document.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.