7.1. Importing ActiveX Controls


ActiveX controls are COM components that you can drop into a form. They may or may not have a user interface. When Microsoft developed the OCX standard, which allowed developers to build ActiveX controls in Visual Basic and use them with C++ (and vice versa), the ActiveX control revolution began. Over the past decade, thousands of such controls have been developed, sold, and used. They are small, easy to work with, and an effective example of binary reuse.

Importing ActiveX controls into .NET is surprisingly easy, considering how different COM objects are from .NET objects. Visual Studio 2005 is able to import ActiveX controls automatically.

As an alternative to using Visual Studio, Microsoft has developed a command-line utility, AxImp, that will create the assemblies necessary for the control to be used in a .NET application.


7.1.1. Creating an ActiveX Control

To demonstrate the ability to use classic ActiveX controls in a .NET application, you'll first develop a simple four-function calculator as an ActiveX control. You'll build the control in VB6, then import it into your Windows Forms application.

If you do not have VB6, you can download the completed project from O'Reilly's site or from http://www.LibertyAssociates.com (click on Books, navigate to this book, and click on thesource code). Once you have the control, you can run Regsvr32 to register it.


To create the control, open VB6 and create a new project, choosing ActiveX Control as the project type. Make the project form as small as possible, because this control will not have a user interface. Right-click UserControl1 and choose Properties. Rename it Calculator in the Properties window. Click the Project in the project explorer, and in the Properties window, rename it CalcControl. Immediately save the project and name both the file and the project CalcControl, as shown in Figure 7-1.

Figure 7-1. Creating a VB6 control


Now, you can add the four calculator functions by right-clicking the CalcControl form, selecting View Code from the pop-up menu, and typing in the VB code shown in Example 7-1.

Example 7-1. Implementing the ActiveX control in VB6
 Public Function _ Add(left As Double, right As Double) _ As Double     Add = left + right End Function Public Function _ Subtract(left As Double, right As Double) _ As Double     Subtract = left - right End Function Public Function _ Multiply(left As Double, right As Double) _ As Double     Multiply = left * right End Function Public Function _ Divide(left As Double, right As Double) _ As Double     Divide = left / right End Function 

This is the entire code for the control. If you want to test this in your VB6 environment before importing it into .NET, compile your control to the file CalcControl.ocx by choosing File Make CalcControl.ocx on the Visual Basic 6 menu bar. Alternatively, you can drag it onto your .NET form (covered next) and .NET will build and register the control for you.

7.1.2. Importing a Control in .NET

Add a new form to the NorthWindWindows application named frmActiveX . Modify the menu on Welcome so that Clock and ActiveX are sub-menu choices under a new menu choice Fun, as shown in Figure 7-2.

Figure 7-2. Adding ActiveX menu choice


Go back to frmActiveX and add the necessary controls to test the ActiveX Control (two text boxes, four buttons, and a label), as shown in Figure 7-3.

7.1.2.1. Importing a control

To import the control, choose Tools Choose Toolbox Items.... When the Choose Toolbox Items menu opens, select the COM Components tab and find the CalcControl.Calculator object you just registered, as shown in Figure 7-4.

Figure 7-3. Test form for ActiveX Control


Figure 7-4. Choose Toolbox Items dialog


Because CalcControl is registered on your .NET machine, the Visual Studio 2005 Customize Toolbox is able to find it. When you select the control from this dialog box, it is imported into your application; Visual Studio takes care of the details, including adding it to your toolbar, as shown in Figure 7-5.

Figure 7-5. CalcControl added to toolbar


Now you can drag this control onto your Windows Form and make use of its functions.

Add event handlers for each of the four buttons. The event handlers will delegate their work to the ActiveX Control you wrote in VB6 and imported into .NET.

The source code for the event handlers is shown in Example 7-2.

Example 7-2. Implementing handlers that use the CalcControl ActiveX Control
 Private Sub btnAdd_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAdd.Click     Dim left As Double = Double.Parse(txtLeft.Text)     Dim right As Double = Double.Parse(txtRight.Text)     lblResults.Text = Me.AxCalculator1.Add(left, right) End Sub Private Sub btnSubtract_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSubtract.Click     Dim left As Double = Double.Parse(txtLeft.Text)     Dim right As Double = Double.Parse(txtRight.Text)     lblResults.Text = Me.AxCalculator1.Subtract(left, right) End Sub Private Sub btnMultiply_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnMultiply.Click     Dim left As Double = Double.Parse(txtLeft.Text)     Dim right As Double = Double.Parse(txtRight.Text)     lblResults.Text = Me.AxCalculator1.Multiply(left, right) End Sub Private Sub btnDivide_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDivide.Click     Dim left As Double = Double.Parse(txtLeft.Text)     Dim right As Double = Double.Parse(txtRight.Text)     lblResults.Text = Me.AxCalculator1.Divide(left, right) End Sub 

Each implementing method obtains the values in the text fields, converts them to Double using the shared method Double.Parse, and passes those values to the calculator's methods. The results are cast back to a string and inserted in the label, as shown in Figure 7-6.

Figure 7-6. Running the ActiveX Control




Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net