Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Using ActiveX Controls with Visual J++

We’re going to get our hands dirty by using one of the controls that Visual J++ supplies. Visual J++ came with 15 ActiveX controls including a Calendar control, a TreeView control, and a Sprite control.

Let’s start by creating a Visual J++ application named UseActiveX1. When the project is created, select the ActiveX Controls tab in the Toolbox window. The ActiveX controls that come with Visual J++ will appear as shown in Figure 20.1.


Figure 20.1  The Toolbox has an ActiveX Controls tab that contains controls that come with Visual J++.

Select the Calendar control in the Toolbox window. Place the control on your Form window so that it occupies the entire Form window as shown in Figure 20.2.


Figure 20.2  Here, we’ve placed a Calendar control in the Form window so that it occupies almost the entire window.

You should notice in Figure 20.2 that the Properties window contains the properties for the Calendar control. Of course, if you select any other control or item and the Calendar control isn’t selected, the Properties window will no longer contain the properties for the Calendar control.

The properties that you’ll most often modify are the showDateSelector, showDays, showHorizontalGrid, showTitle, and showVerticalGrid properties. They all default to a value of true, but you can change the appearance of the control by changing any of them to false. The odd thing about this control is that when you try to change these properties, a button with an ellipsis appears. This button then brings up a dialog box in which you can edit these properties as shown in Figure 20.3.


Figure 20.3  A dialog box appears in which you can set important Calendar control properties.

You’ll also be interested in the events for ActiveX controls. Select the control so that its properties appear in the Properties window. You can then click the Events button (the one with the lightening icon) and the controlis events will appear in the Properties window.

To use the Calendar control, there are two approaches that you can take. You can use the Calendar control’s methods to get the date information from somewhere in your program. The following example shows you how to get the day, month, and year from a Calendar control:

int nDay = calendar1.getDay();
int nMonth = calendar1.getMonth();
int nYear = calendar1.getYear();

You can also add an event handler for a Calendar control. The following event handler is the one I use most often:

private void calendar1_afterUpdate(Object source, Event e)
{
    // Handle the calendar event.
}

Installing the Controls

What if your friend just gave you an ActiveX control that does exactly what your program needs? How are you going to use it in your program? That’s easy; all you have to do is create a COM wrapper for the control.

First things first. I’m going to be using two controls in this section. One was written in Visual Basic and the other in Visual C++. To use the Visual Basic control, you’ll need to have the Visual Basic runtime support installed. To get these two controls, you can go to my Web site at www.infinitevision.net, go to the book section, and find the Learn Visual J++ 6 in 21 Days page. You’ll see at the bottom of the page a link that lets you download the controls for Chapter 20.

After you download the controls for this chapter, unzip them to a directory on your hard drive.


Note:  To unzip files, you’ll need a copy of WinZip. It’s a shareware program that manages .zip files. To obtain a copy, go to www.winzip.com and download it. If you use it regularly, you should register it because it’s shareware.

With the two controls unzipped, you’ll have to register them. The easiest way to do this is from a command prompt. Run a command prompt and log on to the directory into which the controls have been placed. For Windows 95 and Windows 98 systems type the following lines from the command prompt to register the controls.

c:\windows\system\regsvr32 RegistryControl.dll
c:\windows\system\regsvr32 OhmsCalculator.ocx

After you register each control, the regsvr32 program will display whether the registration was successful or whether an error was encountered as shown in Figure 20.4.


Figure 20.4  When you try to register an ActiveX control, the regsvr32 program will show you the results.

When you want to unregister a control, you can use the regsvr32 program. All you need to do is add the /u command line switch as follows:

c:\windows\system\regsvr32 /s RegistryControl.dll

For Windows NT systems type the following lines from the command prompt to register the controls.

c:\winnt\system32\regsvr32 RegistryControl.dll
c:\winπystem32\regsvr32 OhmsCalculator.ocx

A summary of the steps to get and register the controls follows:

1.  Go to www.infinitevision.net.
2.  Follow the Books page to the Learn Visual J++ 6 in 21 Days page.
3.  Look at the bottom of the page and click on the Download the controls for chapter 20 link.
4.  Save the Chap20Controls.zip file (that downloads as a result of clicking on the link) to a directory on your hard drive.
5.  Open a command prompt.
6.  Use the regsvr32 program with each control as a command line parameter.

Now that the controls are on your hard drive and have been registered, you can create a COM wrapper for them and use them in your program. For this discussion, I created a new Visual J++ application named UseActiveX2. I suggest you do the same.

Select Add COM Wrapper from the Project menu. A dialog box will appear in which all of the ActiveX controls that are registered on the system are listed. Find the OhmsCalculator control and check it as shown in Figure 20.5.


Figure 20.5  Select the OhmsCalculator control from the list.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.