Partager via


Visual Basic Concepts

Showing Forms from the CoffeeMonitor Class

Out-of-process components can show both modal and modeless forms. The first use for the CoffeeMonitor will be to demonstrate this, because the results may not be quite what you expect.

The procedure in this topic adds a ShowForm method to the CoffeeMonitor class, plus public constants for use with the method.

Note   This topic is part of a series that walks you through creating a sample ActiveX EXE. It begins with the topic Creating an ActiveX EXE Component.

To show forms from the CoffeeMonitor class

  1. In the Project Explorer window, double-click CoffeeMonitor (or select it and then click the Code button on the Project Explorer window toolbar), to open its code window.

  2. In the Declarations section, add the following Public Enum:

    Option Explicit
    Public Enum cfeModality
       cfeModal = vbModal
       cfeModeless = vbModeless
    End Enum
    

    An enumeration declared Public in a class module is added to your component’s type library. It will not be associated with the class in which it was defined, but will become part of the global name space.

    Why would you provide your own constants, when Visual Basic includes vbModal and vbModeless? Your component may be used with a development tool that doesn’t provide these constants. Providing constants compatible with Visual Basic is a flexible solution.

    Note   Putting the prefix "cfe" in front of the constant names identifies the constants as belonging to the Coffee component, and reduces the chance of name collisions with other components. Some component authors follow the prefix with two or three uppercase letters identifying the Enum; this seems superfluous here. See "Providing Named Constants for Your Component" in "General Principles of Component Design."

  3. On the Tools menu, click Add Procedure to open the Add Procedure dialog box. Type ShowForm in the Name box, click Sub and Public, and then click OK.

    A public Sub or Function procedure in a class module defines a method of the class, while a public property procedure defines a property. See "Adding Properties and Methods to Classes," in "General Principles of Component Design."

  4. Add the following code to the Sub procedure:

    Public Sub ShowForm(Optional Modality As _
          cfeModality = cfeModal)
       Dim frm As New TestForm
       If Modality = cfeModeless Then
          frm.Caption = "TestForm - Modeless"
       Else
          frm.Caption = "TestForm - Modal"
       End If
       frm.Show Modality
    End Sub
    

    The typed optional argument Modality specifies a Modal form if the argument is omitted. Typed optional arguments are discussed in "Passing Arguments to Procedures" in "Programming Fundamentals" in the Visual Basic Programmer’s Guide.

    Because the constants in cfeModality are compatible with the vbModal and vbModeless constants supplied by Visual Basic, you can simply pass the Modality argument to the form’s Show method. The Show method will automatically raise an error if an invalid value is supplied.

  5. On the File menu, click Make Coffee.exe to create a reference executable.

    The reference executable, which you only need to create once, will help your test application keep its connection to this project. See "Creating a Test Project for an Out-of-Process Component" in "Debugging, Testing, and Deploying Components."

  6. Press CTRL+F5 to run the project.

    Note   If Compile On Demand is checked (on the General tab of the Options dialog box, accessible from the Tools menu), you should use CTRL+F5 (or select Start with Full Compile from the Run menu) to ensure that your component is fully compiled before you begin testing. A compilation error that occurs after the component is providing objects to the test program can be very awkward. Compile On Demand is checked by default.

    Important   You must put your project in run mode before editing or running the test program, as explained in "Creating a Test Project for an Out-of-Process Component" in "Debugging, Testing, and Deploying Components."

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX EXE.

To See
Go to the next step Creating the CoffeeWatch Test Project
Start from the beginning Creating an ActiveX EXE Component