Custom ActiveX Controls

The WeightEdit WFC-style control is available to other Visual J Plus Plus v6 apps. (Remember to copy the .class files to the Windows\Java\Classes directory so that the .EXE files generated using the WeightEdit control can find them.) However, only Visual J Plus Plus can use WFC controls. Is there any way to make the WeightEdit control more universally available?

Turning the WeightEdit control into an ActiveX control makes it available to other Visual Studio 6 languages, not to mention any other ActiveX-capable app. (It's doubtful that any app other than a coding language would know what to do with the WeightEdit ActiveX control.)

Turning WeightEdit into an ActiveX Control

Once you've created a WFC-style control, the remaining steps needed to create an ActiveX control are straightforward:

  1. Uncomment the @com.dll directive.
  2. Repackage the .class files and type library (.tlb) file into a COM package.
  3. Rebuild the project.
  4. Register the COM package as an ActiveX component.
  5. Add the ActiveX control to the Toolbox.

Preparing the WFC-style control

There's no reason to retain the WeightEditTest project in the solution, because it won't be made part of the ActiveX control. Right-click the WeightEditTest project in Project Explorer, and select Remove Project. Removing the project doesn't delete the files or the directory that make up the project.

You might also want to change from the Debug configuration to the Release configuration, which will reduce the size of the eventual .class files by removing the debug information. Right-click the project, and select WeightEdit Properties. Select Release from the Configuration drop-down list.

NOTE
We could have done this at any point during the creation of the WFC-style control.

Uncommenting the DLL directive

Edit the WeightEdit.java file, and remove the // comment bars that precede the @com.register directive. This assigns the control a GUID, which uniquely identifies our WeightEdit control.

Repackaging the project

The control will need all of the .class files that are part of the WeightEdit project. To make sure that all the .class files are included, open the WeightEdit Properties dialog box again. In the COM Classes tab, check each of the classes present. Figure 13-9 shows the resulting display.

Now click the Output Format tab. Check Enable Packaging. From the Packaging Type dropdown list, select COM DLL. Figure 13-10 shows the WeightEdit Properties dialog box with these settings selected.

Java Click to view at full size.

Screenshot-9. The COM Classes tab allows you to select which classes you want to include in the ActiveX control.

Java Click to view at full size.

Screenshot-10. The Output Format tab allows you to bundle the necessary files into a single COM DLL.

Now close the Properties window and rebuild the project. The @com.dll directive tells Visual J Plus Plus v6 to register the WeightEdit COM DLL as an ActiveX component.

Adding the ActiveX control to the Toolbox

To add the new control to the Toolbox, right-click within the General section of the Toolbox. From the context menu, select Customize Toolbox. The steps so far are the same as those for creating a WFC-style control.

Click the ActiveX Controls tab rather than the WFC Controls tab we used in the previous section. Scroll down to the bottom of the list of available ActiveX controls to find our WeightEdit control, as shown in Figure 13-11. Check the box to create the new Toolbox control. A new icon will appear on the Toolbox immediately below the WFC control icon (assuming you haven't already removed the WFC-style control icon from the Toolbox). The ActiveX control appears disabled because there is no app in which to use the control.

NOTE
The ActiveX control icon is slightly different than the WFC-style control icon. This difference distinguishes the two types of controls for the user.

You can now close the WeightEdit solution.

Java Click to view at full size.

Screenshot-11. The Customize Toolbox dialog box shows the available ActiveX controls.

Using the new ActiveX control

We can use the new WeightEdit ActiveX control in much the same way as we used the WFC-style control we created earlier in the chapter. The main difference between the WFC-style control and the ActiveX control is that Visual J Plus Plus v6 creates a set of interface classes with which to access the ActiveX control.

To demonstrate this difference, create a new Windows app named WeightEditTest. Double-click Form1.java in Project Explorer to open the class in the Forms Designer (if it isn't open already). Open the Toolbox. Now that an app is open, the WeightEdit ActiveX control is enabled. Drag the control and drop it in the middle of the WeightEditTest form.

You should notice the large amount of disk activity as soon as you drop the WeightEdit ActiveX control onto the form. (By comparison, adding the WFC-style control was much quicker.) This activity is due to the creation of the interface classes mentioned previously.

Look at the project with Project Explorer. Figure 13-12 shows just a subset of new files added to the project. Each of these small .java files represents an active property in the WeightEdit ActiveX control.

Open the Properties window, and select the WeightEdit control within Form1. As before, both the weight and weightEvent properties are present. Build and run the WeightEditTest app. The WeightEditTest app runs in the same way it did before, although it takes somewhat longer to begin running because of the increased access to the operating system ActiveX components require. If you examine the WeightEditTest.exe file, you'll notice that it weighs in at over 100 KB—large for a Java app, but still much smaller than a C or C++ program.

There is one difference between the WFC-style control and the ActiveX control. Select the WeightEdit control in the Forms Designer. Now begin typing. Rather than update the control, the Properties window immediately appears in the foreground. Whatever text you enter appears in the text property. The text that appears in the ActiveX control doesn't update until you finish updating the text property.

NOTE
You'll find this version of the WeightEdit control on the companion CD in the subdirectory Custom Controls\ActiveX WeightEdit.

Java Click to view at full size.

Screenshot-12. Visual J Plus Plus v6 generates a separate interface class for each event handler in the WeightEdit ActiveX control.

Using the ActiveX control in Visual Basic 6

As mentioned earlier, an ActiveX control written in Visual J Plus Plus v6 is available to all other Visual Studio 6 languages. To demonstrate this versatility, I created the default Windows EXE program under Visual Basic 6 using the following steps. (If you own Visual Basic 6, you can follow along; if not, you'll just have to trust me.)

Open Visual Basic 6. Create a new Standard EXE. Open Form1 within Project1 in the Forms Designer.

You'll need to add the WeightEdit control to the Visual Basic 6 Toolbox before you can use it. Right-click within the Visual Basic Toolbox. From the context menu, choose Components to bring up the Components dialog box. The WeightEvent ActiveX component is at the bottom of the list, as shown in Figure 13-13. Check the WeightEdit component, and choose OK to close the window. A new control appears in the Visual Basic Toolbox.

NOTE
The display on my computer reflects the fact that I had already created a different WeightEdit component. Your display might list only a single WeightEdit component. In any case, you want to use the component that references MSJAVA.DLL, as shown in Figure 13-13.

Select the newly added WeightEdit control. Now click somewhere within the form. While holding down the left mouse button, drag the mouse to specify the size of the WeightEdit control. Type within the object to add text and update the text property in the Properties window, as shown in Figure 13-14.

Java Click to view at full size.

Screenshot-13. Visual Basic allows you to add Visual J++ controls to the Toolbox.

Java Click to view at full size.

Screenshot-14. Typing within the WeightEdit control updates the text property, just as within Visual J Plus Plus v6.

Scroll down within the Properties window to the weight property. Change the weight to Bold. As you can see in Figure 13-15, the text display immediately updates to reflect the change.

Java Click to view at full size.

Screenshot-15. Updating the weight property updates the text display within the Form1 window.

To access the weightEvent property, double-click the WeightEdit control. A code window displaying the WeightEdit1_click() subroutine appears. Open the drop-down list on the upper right-hand side of the code window. Scroll down the list to find the weightEvent property. Selecting that list item creates the WeightEdit1_weightEvent() subroutine shown in Figure 13-16.

Finally, from the Run menu, select Start With Full Compile to run the Visual Basic 6 program. As the program runs, a form containing the WeightEdit control appears. Click the control, and begin typing to verify that it functions as designed.

NOTE
You'll find this version of using the WeightEdit control on the companion CD in the subdirectory Custom Controls\Visual Basic Program.

Java Click to view at full size.

Screenshot-16. The weightEvent property is available in the code editor. Comments