You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q315847
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


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.
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Create a new Console Application in Visual Basic .NET or in Visual Basic 2005.
  3. On the Project menu, click Add Reference.
  4. 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.
  5. 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.
  6. In the Code window of Module1.vb, add the following code to the Sub Main procedure:
    Dim myActiveX As MyActiveXComponent.MyClassmyActiveX = New MyActiveXComponent.MyClass()Dim mySum As IntegermySum = myActiveX.Add(1, 2)Console.Write("1 + 2 = " & 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.
  7. On the Debug menu, click Start to test the application. The following output appears in the Console window:
    1 + 2 = 3						
back to the top

Complete Visual Basic .NET Code Listing

Option Explicit OnOption Strict OnModule Module1	Sub Main()		Dim myActiveX As MyActiveXComponent.MyClass   	myActiveX = New MyActiveXComponent.MyClass()   	Dim mySum As Integer   	mySum = myActiveX.Add(1, 2)   	Console.Write("1 + 2 = " & CStr(mySum))   	Console.ReadLine()		End SubEnd 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: MyClassOption ExplicitPublic Function Add(ByVal Num1 As Long, ByVal Num2 As Long) As Long  Add = Num1 + Num2End Function				
back to the top


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
For more information, refer to the following Microsoft Web sites:
.NET Interop: Get Ready for Microsoft .NET by Using Wrappers to Interact with COM-based Applications

Using ActiveX Controls with Windows Forms in Visual Studio .NET
back to the top

Article ID: 315847 - Last Review: 11/27/2007 02:31:18 - Revision: 3.6

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

  • kbvs2005swept kbvs2005applies kbhowtomaster KB315847