Click here to Skip to main content
14,027,272 members
Click here to Skip to main content
Add your own
alternative version


83 bookmarked
Posted 31 May 2007
Licenced CPOL

Using .NET Controls in VB6

, 31 May 2007
Rate this:
Please Sign up or sign in to vote.
An article on using .NET controls in VB6

Screenshot - screenshot.jpg


Recently, a customer asked if it would be possible to add some specific functionality to a program written by us. The program in question displayed the forces and streamed in real-time, acting on the various axles of a racing car. The version at the time simply had a PictureBox control in VB6 with text boxes, etc. positioned around the picture to display the values. The customer was requesting that this whole "picture" be scaled depending on the size of the window, so that on large screens it would be easier to read.

The Problem

Put quite simply, the language in which the application was written, VB6, does not provide very good support for graphics. This is especially the case for resizing pictures proportionally, which was the one major client requirement.

The Solution

The time frame for implementing the solution was limited, so although not completely out of the question, a complete re-write of the software in VB.NET was not really feasible. One possibility which presented itself was to see if a control written using .NET could be used in a VB6 application using COM Interop. As it turned out, this is quite easy, but the leg-work involved behind it revealed quite a few dead-ends. So, it is the aim of this article to eliminate those dead-ends for other people wishing to accomplish the same thing.

Stage 1: Creating the Control

This took the longest of the three stages in our case, simply because of the nature of the control and calculating the positions of where text, etc. was meant to be positioned. I will not delve into the details of our control, but only the steps that are required for VB6 Interop.

  1. Create a new Windows Control Library project from within Visual Studio.

    Screenshot - stage1step1.gif

  2. In both the Debug and Release modes of the Property Pages, set the "Register for COM Interop" checkbox.

    Screenshot - stage1step2.gif

  3. Inside the AssemblyInfo.cs file, change the assembly wide attribute ComVisible to true. If it's not already in the configuration file, add it.
    [assembly: ComVisible(true)]

That is all that is required to make the project visible to VB6 projects.


A quick word about these: properties are exposed to VB6 so, like .NET controls, if you want to expose a value, you must wrap it in a Property expression. You cannot just have it visible as a field.

Stage 2: Registering the Assembly

The library must be registered on the client machine before use by VB6. If it is not registered on the development machine, then it will not be visible in the References dialog of VB6. If it's not registered on the installation machine, then it is a similar problem to if you have not registered a classic DLL or ActiveX control. The "Register for COM Interop" checkbox in VS2005 performs this registration automatically while the environment is running, but un-registers it when VS is closed.

To register the assembly, you must use the .NET equivalent of regsvr32, regasm. This is located in the framework directory, usually "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727". To register it, open a command prompt and run the following command, assuming that the framework directory and the assemblies directory are in the environment's current path.

regasm.exe Assembly.dll

Stage 3: Adding to VB6 Projects

The secret here is the VBControlExtender object, which allows a .NET control to be hosted on a VB6 form. However, the first stage is to add a reference to the assembly you just registered. This is accomplished by checking the box in the Project | References menu.

Screenshot - references.gif

Once that has been accomplished, then the following code can be added to the form's code in the project:

Option Explicit
Dim car As VBControlExtender

Private Sub Form_Load()
    Set car = Controls.Add("CarControl.Car", "car", Me)
End Sub

The other code included in the demo's source file simply resizes the control based on the form size and sets random values to the properties of the control.

Private Sub Form_Resize()
    car.Left = 100
    car.Width = Me.Width - 300
    car.Top = 100
    car.Height = Me.Height - 700
    car.Visible = True
End Sub

Private Sub timer_Timer()
    ' Randomise the timer
    ' Generate random numbers
    car.object.FrontL = Rnd()
    car.object.FrontR = Rnd()
    car.object.RearL = Rnd()
    car.object.RearR = Rnd()
End Sub

You'll notice that I have had to refer to the properties of the .NET control through car.object. This provides late-binding functionality for VB6. All COM-Visible methods in the .NET control are accessible through this object. You just have to know what you're typing because it is late-bound.


Hopefully, my ability (or lack thereof) as an article writer hasn't masked the important bits of the article so much that it is unusable. I personally find that the code speaks for itself and have included all .NET & VB6 code in the attached ZIP files, so do take a moment to browse through them.


  • 31st May, 2007 -- Original version posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Engineer PooreDesign
United Kingdom United Kingdom
Ed is a student who due to a form of cancer (now clear) took a year out before going to Imperial College, London to study Electronic Engineering.

His interests include shooting (clay-pigeon (shotgun), air-rifle and rifle), playing with his three labradors (Sandy, Rosie and Tundra), programming (most experienced in C# and C, although those are not the only ones), walking (has completed Gold Duke of Edinburgh's Award), playing games and reading.

He lives in two places on a 57 acre farm in West Waleswith the rest of the family during the holidays; and Greater London during term time.

Languages and Technologies: C#, C, VB6, VB.NET, XAML, (X)HTML, CSS, XSLT, Assembler (PIC), ASP.NET, WPF, Windows.Forms, ASP, VBScript, JavaScript, Pascal / Delphi, XML

Current Stuff:
1st Year MEng Electronics Engineering (Imperial College, London)

You may also be interested in...


Comments and Discussions

QuestionPublic properties of the control Pin
Axis236-Jun-16 4:43
memberAxis236-Jun-16 4:43 
QuestionVB6 is extremely powerful Pin
ISpliter3-Mar-14 13:40
memberISpliter3-Mar-14 13:40 
QuestionGives Run-time Err Pin
arunrk8715-Oct-13 3:33
memberarunrk8715-Oct-13 3:33 
AnswerRe: Gives Run-time Err Pin
arunrk8717-Oct-13 20:26
memberarunrk8717-Oct-13 20:26 
GeneralMy vote of 2 Pin
span23724-Feb-10 11:34
memberspan23724-Feb-10 11:34 
GeneralNice article i was able to acheive using a .net control in vb6 Pin
Member 430909427-Oct-09 3:04
memberMember 430909427-Oct-09 3:04 
QuestionHow to use the usercontrol COM interop in MFC? Pin
materatsu18-Nov-08 7:38
membermateratsu18-Nov-08 7:38 
AnswerRe: How to use the usercontrol COM interop in MFC? Pin
Ed.Poore18-Nov-08 22:20
memberEd.Poore18-Nov-08 22:20 
GeneralWell done and thanks Pin
ap_wooka9-Apr-08 6:02
memberap_wooka9-Apr-08 6:02 
QuestionProblems deploying the VB6 App with .net .dll Pin
Jase_rw25-Oct-07 6:46
memberJase_rw25-Oct-07 6:46 
AnswerRe: Problems deploying the VB6 App with .net .dll Pin
Ed.Poore25-Oct-07 7:30
memberEd.Poore25-Oct-07 7:30 
GeneralRe: Problems deploying the VB6 App with .net .dll [modified] Pin
diaak5220-Oct-08 20:44
memberdiaak5220-Oct-08 20:44 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Ed.Poore20-Oct-08 21:13
memberEd.Poore20-Oct-08 21:13 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Internet Filter12-Jan-10 16:42
memberInternet Filter12-Jan-10 16:42 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Ed.Poore12-Jan-10 21:44
memberEd.Poore12-Jan-10 21:44 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Jason_Weier9-May-12 7:15
memberJason_Weier9-May-12 7:15 
GeneralGreat article Pin
mmsinclair2-Oct-07 6:57
membermmsinclair2-Oct-07 6:57 
GeneralRe: Great article Pin
Ed.Poore2-Oct-07 12:45
memberEd.Poore2-Oct-07 12:45 
Question.Net (User) Control & VB5 Pin
grxie11-Jun-07 10:17
membergrxie11-Jun-07 10:17 
AnswerRe: .Net (User) Control & VB5 Pin
Ed.Poore11-Jun-07 10:23
memberEd.Poore11-Jun-07 10:23 
QuestionEvents? Pin
tassieboy5-Jun-07 14:57
membertassieboy5-Jun-07 14:57 
AnswerRe: Events? Pin
Ed.Poore5-Jun-07 19:26
memberEd.Poore5-Jun-07 19:26 
GeneralRe: Events? Pin
tassieboy5-Jun-07 19:43
membertassieboy5-Jun-07 19:43 
GeneralRe: Events? Pin
Ed.Poore5-Jun-07 19:52
memberEd.Poore5-Jun-07 19:52 
AnswerRe: Events? Pin
ap_wooka10-Apr-08 11:46
memberap_wooka10-Apr-08 11:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190419.4 | Last Updated 31 May 2007
Article Copyright 2007 by Ed.Poore
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid