VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 14

Creating an ActiveX Project With Multiple UserDocument Objects

Many Active Document projects will have more than one type of Active Document. In these cases, you will have two or more UserDocument objects, and it may be necessary to pass data between the two documents. This can occur when one document has data upon which the other document depends.

Additionally, with Active Documents, you do not necessarily know in what sequence your Active Document was invoked. You might expect that the normal course of your application is to use document A, for example, and then document B. However there is no automatic way to prevent the user from going to document B directly. If you have code in document B that depends on data from document A, you could have some problems.

This behavior can be controlled using global variables to pass between documents. Consider a VB project made up of three files: a Standard Code module and two UserDocument modules. In the Code module, a global variable called gobjCurrentDocument is defined as an object. This variable is used to pass the document object references between documents. The entire contents of the Standard module reads as follows:

Option Explicit
Public gobjCurrentDocument As Object

The two UserDocuments are for the most part identical; they both display each other's data. The code listing for one UserDocument is shown as follows (the code listing for the second UserDocument would simply contain changed references to the "other" UserDocument):

Public Property Get DocText()
   'Return the current document's code
   DocText = txtMyDoc.Text
End Property

Private Sub Command1_Click()
   'Navigate to a new document
   Set gobjCurrentDocument = Me
   Hyperlink.NavigateTo App.Path & "\usrdoc2.vbd"
End Sub

Private Sub txtMyDoc_Change()
   PropertyChanged "DocText"
End Sub

Private Sub UserDocument_ReadProperties(PropBag As PropertyBag)
   txtMyDoc.Text = PropBag.ReadProperty("DocText", "")
End Sub

Private Sub UserDocument_Show()
   'When show is called then get the other documents
   'data using the global variable
   If gobjCurrentDocument Is Nothing Then
      txtOutPutFromOtherDoc.Text = "No Older Document"
      txtOutPutFromOtherDoc.Text =
      'destroy the object reference
      Set gobjCurrentDocument = Nothing
   End If
End Sub

Private Sub UserDocument_WriteProperties(PropBag As PropertyBag)
   PropBag.WriteProperty "DocText", txtMyDoc.Text, ""
End Sub

Each module has only the following procedures:

  • The DocText property is used to return the document's data.

  • The Command1_Click event sets the global variable to the current document and then navigates to the other document.

  • The Show event is fired when the document is displayed. The global variable is checked, and the data from the other document is shown on a No Older Document message.

  • The Change event of the TextBox calls the PropertyChanged method to alert the system that the DocText property has changed.

  • If there has been a change in the DocText property, the WriteProperties event fires when the container closes the document.

  • The ReadProperties event fires each time the document is resited in its container.

When the application is executed in an Internet Explorer container, a window appears, as shown in Figure 14.5.

The first document opened.
FIGURE 14.5 The first document opened.

Notice that the Document Two text box is set to No Older Document. This means that the global variable is set to Nothing, indicating that an older instance of Document Two has not yet run during this session. When the user presses the CommandButton, a window appears, as shown in Figure 14.6.

The second document opened.
FIGURE 14.6 The second document opened.

Notice that the second text box now has Document One's data. You could further enhance this method to enforce the sequence in your Active Documents. If the global variable were not equal to a predetermined value, you could then notify the user and navigate to the appropriate document.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved