Using ActiveX Controls on VB .NET Forms

ActiveX controls are also COM objects and you must rely on COM interop to continue using these controls on your Visual Basic .NET forms. As with other COM objects, an RCW is required to work with ActiveX controls. Again, you have two ways of creating RCWs for ActiveX controls—using Visual Studio .NET or using a command-line utility that ships with the .NET Framework called AxImp.exe.

Let's start with using Visual Studio.NET to create an RCW for an ActiveX control, which in this case is the Microsoft MAPI Session control. This control allows you to perform MAPI operations through a form control versus using CDO's object model. Perform the following steps to add the MAPI Session ActiveX control to your project's toolbox.

1.
Open or create a new Visual Basic. NET program and select Add/Remove Toolbox items from the Tools menu. This will display the Customize Toolbox dialog box, as shown in Figure 9.3.

Figure 9.3. Adding an ActiveX control to the Visual Studio .NET control toolbox.


2.
Click the COM Components tab in the Customize Toolbox dialog box to display a list of all of the ActiveX controls registered on your computer.

3.
From the list of ActiveX controls, click the check box next to the Microsoft MAPI Session Control, version 6.0.

4.
Click OK to add the control to your toolbox.

5.
Drag the control to a form.

Once you have dragged the control to the form, you can set its properties and call its methods as you normally would any control.

Just as when you added a COM object reference to your project, an RCW was created to help facilitate communication between your managed application and the ActiveX control. If you look under References in your Solution Explorer, you should see two new entries—AxMSMAPI and MSMAPI. Windows forms can be a host only to Windows Forms controls, which are derived from System.Windows.Forms.Control. The AxMSMAPI assembly is a Windows Forms proxy for ActiveX controls and allows the underlying ActiveX control to act as a Windows Forms control in a .NET application. The MSMAPI reference is the RCW for the underlying ActiveX control and is responsible for calling the ActiveX control directly. Visual Studio .NET also copies these files into your project's directory, thus making them local to your project.

The AxImp utility, shipped in the .NET Framework SDK, is another method you can use to create wrappers for you ActiveX controls so that they can be consumed by your .NET forms. Just like TLBImp, AxImp is a command-line utility that is considerably more functional than Visual Studio when it comes to the creation of the wrapper assemblies. With AxImp, you can specify the name and location of the output files as well as provide a strong name for your wrapper assemblies, as shown below. In the example, the AxMSMAPI and MSMAPI assemblies are created in C:WindowsSystem32 directory and the assemblies are given a strong name using the cryptographic information provided in the keyfile called MyCompany.snk. Similar to RCW created with TLBImp, if you want to have these assemblies made available to many different .NET applications, you can install the assemblies into the GAC.

aximp c:windowssystem32msmapi32.ocx /keyfile:MyCompany.snk

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.47.169