Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using .NET Controls in VB6

0.00/5 (No votes)
31 May 2007 1  
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.
    [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:

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

  • 31st May, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here