Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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 timeframe 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.

Properties

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\Frmaework\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
    Randomize
    ' 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.

Summary

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.

History

  • 31 May, 2007 -- Original version posted

License

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

Share

About the Author

Ed.Poore
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)

Comments and Discussions

 
QuestionVB6 is extremely powerful PinmemberISpliter3-Mar-14 13:40 
QuestionGives Run-time Err Pinmemberarunrk8715-Oct-13 3:33 
AnswerRe: Gives Run-time Err Pinmemberarunrk8717-Oct-13 20:26 
GeneralMy vote of 2 Pinmemberspan23724-Feb-10 11:34 
GeneralNice article i was able to acheive using a .net control in vb6 PinmemberMember 430909427-Oct-09 3:04 
QuestionHow to use the usercontrol COM interop in MFC? Pinmembermateratsu18-Nov-08 7:38 
AnswerRe: How to use the usercontrol COM interop in MFC? PinmemberEd.Poore18-Nov-08 22:20 
GeneralWell done and thanks Pinmemberap_wooka9-Apr-08 6:02 
I think the caption says it all really.
I am used to writing in C# and have recently been thrown into a VB6 world. As a custom control junkie this will allow me to feed my habit while in a rather inhospitable environment.
 
Thanks again,
Wooka
QuestionProblems deploying the VB6 App with .net .dll PinmemberJase_rw25-Oct-07 6:46 
AnswerRe: Problems deploying the VB6 App with .net .dll PinmemberEd.Poore25-Oct-07 7:30 
GeneralRe: Problems deploying the VB6 App with .net .dll [modified] Pinmemberdiaak5220-Oct-08 20:44 
GeneralRe: Problems deploying the VB6 App with .net .dll PinmemberEd.Poore20-Oct-08 21:13 
GeneralRe: Problems deploying the VB6 App with .net .dll PinmemberInternet Filter12-Jan-10 16:42 
GeneralRe: Problems deploying the VB6 App with .net .dll PinmemberEd.Poore12-Jan-10 21:44 
GeneralRe: Problems deploying the VB6 App with .net .dll PinmemberJason_Weier9-May-12 7:15 
GeneralGreat article Pinmembermmsinclair2-Oct-07 6:57 
GeneralRe: Great article PinmemberEd.Poore2-Oct-07 12:45 
Question.Net (User) Control & VB5 Pinmembergrxie11-Jun-07 10:17 
AnswerRe: .Net (User) Control & VB5 PinmemberEd.Poore11-Jun-07 10:23 
QuestionEvents? Pinmembertassieboy5-Jun-07 14:57 
AnswerRe: Events? PinmemberEd.Poore5-Jun-07 19:26 
GeneralRe: Events? Pinmembertassieboy5-Jun-07 19:43 
GeneralRe: Events? PinmemberEd.Poore5-Jun-07 19:52 
AnswerRe: Events? Pinmemberap_wooka10-Apr-08 11:46 
GeneralRe: Events? Pinmembertassieboy10-Apr-08 13:22 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 31 May 2007
Article Copyright 2007 by Ed.Poore
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid