Visual Basic Concepts
In-process components can serve as libraries of procedures and dialog boxes, saving you programming time and giving your applications a consistent look and feel.
Note This topic is part of a series that walks you through creating a sample ActiveX DLL. It begins with the topic Creating an ActiveX DLL.
The procedures in this topic demonstrate the way objects can be used to control modal or modeless dialog boxes. The same form is used for both cases, as shown in Figure 2.3.
Figure 2.3 The dlgDemo dialog box
To add a form to the ThingDemo project
Important Whenever you’re working with a project group, make sure the right project is active before adding a new module.
Design the form as you would any Visual Basic form, and save it as ThingDemo_dlgDemo.frm. The following table lists property settings for the objects in the form.
Object | Property | Setting |
Form | Name BorderStyle Caption |
dlgDemo Fixed Dialog Dialog Box |
TextBox | Name Text |
txtDemo (Empty) |
The dialog box is not invoked directly from the client, because forms are private classes. Clients cannot create instances of private classes, and you should never pass instances of private classes to client applications. This is discussed in "Data Types Used in Properties and Methods" in "General Principles of Component Design."
To display the dialog box, clients will call the ShowDialog method of a global Dialogs object, which will create and show the dlgDemo form.
To add code to the dlgDemo form
' Declare an event.
Event NotifyClients(ByVal Data As String)
Private Sub txtDemo_Change()
RaiseEvent NotifyClients(txtDemo.Text)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
' If the Close button was pressed, hide the
' dialog box instead of unloading it.
If UnloadMode = vbFormControlMenu Then
Cancel = True
Me.Visible = False
End If
End Sub
If dlgDemo is shown as a modal dialog, hiding the dialog rather than unloading it allows the ShowDialog method of the Dialogs class to retrieve the value in the text box.
To create the Dialogs class
Dialogs
.Private WithEvents mdlg As dlgDemo
Event NotifyClients(ByVal Data As String)
Private Sub mdlg_NotifyClients(ByVal Data As String)
RaiseEvent NotifyClients(Data)
End Sub
The Dialogs object receives the NotifyClients event from its dlgDemo form whenever the contents of the dialog’s text box changes. The Dialogs object immediately raises its own NotifyClients event, passing along the data to its own client.
Private Sub Class_Initialize()
Debug.Print "Dialogs object created"
Set mdlg = New dlgDemo
End Sub
Private Sub Class_Terminate()
Debug.Print "Dialogs object terminated"
Unload mdlg
Set mdlg = Nothing
End Sub
When terminating, an object that controls a form should always unload the form and set its reference to the form to Nothing, to avoid tying up resources with orphaned forms.
In the code window, change the newly created Function procedure to appear as follows:
Public Function ShowDialog( _
Optional ByVal Text As String = "", _
Optional ByVal Modal As Boolean = True) _
As String
With mdlg
.txtDemo.Text = Text
If Modal Then
.Caption = "Modal Dialog Box"
.Show vbModal
ShowDialog = .txtDemo.Text
Else
.Caption = "Modeless Dialog Box"
.Show vbModeless
End If
End With
End Function
Typed optional arguments let the compiler catch type mismatch errors, instead of waiting until run-time errors occur. Typed optional arguments are discussed in "Programming Fundamentals" in the Visual Basic Programmer’s Guide.
The ShowDialog method that displays the dialog has two optional arguments:
The Text argument is assigned to the text box on dlgDemo before the dialog box is shown. The default value of the Modal argument is True, so omitting it causes the dialog to be modal.
If the dialog is shown Modal, the ShowDialog method returns the contents of the txtDemo
text box after the dialog has been dismissed by the user.
If the dialog is shown Modeless nothing is returned, because the client received a NotifyClients event whenever the contents of the text box changed.
For More Information Events are discussed in "Adding Events to Classes" in "General Principles of Component Design."
This topic is part of a series that walks you through creating a sample ActiveX DLL.
To | See |
Go to the next step | Using the Global Object in TestThing |
Start from the beginning | Creating an ActiveX DLL. |