Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic

Using .NET Controls in VB6

Rate me:
Please Sign up or sign in to vote.
4.60/5 (32 votes)
31 May 2007CPOL4 min read 202.7K   7.4K   86   38
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 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.
    C#
    [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\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:

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

VBScript
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

  • 31st 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)


Written By
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

 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Ed.Poore12-Jan-10 21:44
Ed.Poore12-Jan-10 21:44 
GeneralRe: Problems deploying the VB6 App with .net .dll Pin
Jason_Weier9-May-12 7:15
Jason_Weier9-May-12 7:15 
GeneralGreat article Pin
mmsinclair2-Oct-07 6:57
mmsinclair2-Oct-07 6:57 
GeneralRe: Great article Pin
Ed.Poore2-Oct-07 12:45
Ed.Poore2-Oct-07 12:45 
Question.Net (User) Control & VB5 Pin
grxie11-Jun-07 10:17
grxie11-Jun-07 10:17 
AnswerRe: .Net (User) Control & VB5 Pin
Ed.Poore11-Jun-07 10:23
Ed.Poore11-Jun-07 10:23 
QuestionEvents? Pin
tassieboy5-Jun-07 14:57
tassieboy5-Jun-07 14:57 
AnswerRe: Events? Pin
Ed.Poore5-Jun-07 19:26
Ed.Poore5-Jun-07 19:26 
Adam,

Unfortunately I don't have any insight at the moment it's been one of the areas in which I've meant to have a look, perhaps if I can spare the time I'll have a look today.  I've also wanted to look at how nullable types are handled by VB.

Thanks for providing me with a bit of a kick up the you know what.



GeneralRe: Events? Pin
tassieboy5-Jun-07 19:43
tassieboy5-Jun-07 19:43 
GeneralRe: Events? Pin
Ed.Poore5-Jun-07 19:52
Ed.Poore5-Jun-07 19:52 
AnswerRe: Events? Pin
ap_wooka10-Apr-08 11:46
ap_wooka10-Apr-08 11:46 
GeneralRe: Events? Pin
tassieboy10-Apr-08 13:22
tassieboy10-Apr-08 13:22 
GeneralRe: Events? Pin
ap_wooka11-Apr-08 4:07
ap_wooka11-Apr-08 4:07 
GeneralLooks good but files are missing Pin
tmehta4-Jun-07 18:54
tmehta4-Jun-07 18:54 
GeneralRe: Looks good but files are missing Pin
Ed.Poore5-Jun-07 6:45
Ed.Poore5-Jun-07 6:45 
GeneralRe: Looks good but files are missing Pin
AmitGargash7-Dec-12 19:38
AmitGargash7-Dec-12 19:38 

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.