Search In Site

Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

24 June, 2013

How To Coordinate Data Between MDI Child Forms


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. 

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.


Arrey of forms in Visual Basic


Arrays Of Forms
The user wants to open 20 documents at the same time how can you keep track of all that? Would not 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

 
Twitter Bird Gadget