Microsoft KB Archive/315847

= How to use ActiveX components in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 315847

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q315847



IN THIS TASK
SUMMARY
 * Requirements
 * Using ActiveX Components from Visual Studio .NET
 * Complete Visual Basic .NET Code Listing
 * Complete Code Listing for MyActiveXComponent
 * Troubleshooting

REFERENCES



SUMMARY
This article demonstrates how to use an ActiveX component from within Visual Studio .NET by using Visual Basic .NET or within Visual Studio 2005 by using Visual Basic 2005.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual Basic .NET or Visual Basic 2005

This article assumes that you are familiar with the following topics:
 * Visual Basic .NET or Visual Basic 2005
 * ActiveX

back to the top

Using ActiveX Components from Visual Studio .NET or from Visual Studio 2005
You can use ActiveX components from within Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 code by using the Microsoft .NET Framework Component Object Model (COM) interoperability layer (or COM Interop). With Visual Studio .NET or Visual Studio 2005, you can easily access and use ActiveX components.

NOTE: This article uses a simple ActiveX component named MyActiveXComponent for demonstration purposes. This component contains a single class, MyClass, with a single method, Add. The Add method adds two numbers together and returns the sum. Refer to the Complete Code Listing for MyActiveXComponent section for the source code of this component. The techniques in this article apply to any ActiveX component.  Start Visual Studio .NET or Visual Studio 2005. Create a new Console Application in Visual Basic .NET or in Visual Basic 2005. On the Project menu, click Add Reference. In the Add Reference dialog box, click the COM tab. Notice that the ListView control lists in alphabetical order all of the ActiveX components that are registered on the local computer. In the ListView control, click MyActiveXComponent, click Select, and then click OK. Visual Studio .NET or Visual Studio 2005 automatically creates a wrapper class for the ActiveX component. You can now use this ActiveX component as if it were a native .NET component.

Note In Visual Studio 2005, you do not have to click Select.  In the Code window of Module1.vb, add the following code to the Sub Main procedure: Dim myActiveX As MyActiveXComponent.MyClass myActiveX = New MyActiveXComponent.MyClass

Dim mySum As Integer

mySum = myActiveX.Add(1, 2)

Console.Write(&quot;1 + 2 = &quot; & CStr(mySum)) Console.ReadLine This code declares and creates an instance of the ActiveX component. The code then calls the Add method and displays the sum in the Console window.  On the Debug menu, click Start to test the application. The following output appears in the Console window:

<pre class="fixed_text">1 + 2 = 3

</li></ol>

back to the top

Complete Visual Basic .NET Code Listing
Option Explicit On Option Strict On

Module Module1 Sub Main Dim myActiveX As MyActiveXComponent.MyClass myActiveX = New MyActiveXComponent.MyClass

Dim mySum As Integer

mySum = myActiveX.Add(1, 2)

Console.Write(&quot;1 + 2 = &quot; & CStr(mySum)) Console.ReadLine End Sub End Module back to the top

Complete Code Listing for MyActiveXComponent
'Sample ActiveX Component Source Code 'Written in Visual Basic 6.0 ' 'Project Name: MyActiveXComponent 'Project Type: ActiveX DLL 'File Name: MyActiveXComponent.dll ' 'Class Name: MyClass

Option Explicit

Public Function Add(ByVal Num1 As Long, ByVal Num2 As Long) As Long Add = Num1 + Num2 End Function back to the top

Troubleshooting
As with any other COM component, you must register ActiveX components before you can use them. Make sure that you use a tool such as Regsvr32.exe to register MyActiveXComponent.

back to the top

<div class="references_section">