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

Article translations Article translations
Article ID: 315847 - View products that this article applies to.
This article was previously published under Q315847
Expand all | Collapse all

On This Page

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.

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

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.MyClass
    myActiveX = New MyActiveXComponent.MyClass()
    
    Dim mySum As Integer
    
    mySum = 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
    						

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("1 + 2 = " & CStr(mySum))
   	Console.ReadLine()
	
	End Sub
End Module
				

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
				

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.

REFERENCES

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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmag01/html/INTEROP.asp

Using ActiveX Controls with Windows Forms in Visual Studio .NET
http://msdn2.microsoft.com/en-us/library/ms973200.aspx

Properties

Article ID: 315847 - Last Review: November 27, 2007 - Revision: 3.6
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster KB315847

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com