Multi-verse of Basic: Using VB.Net Control in VB6 Application

You wouldn’t be thinking of doing this unless you’re supporting old/legacy applications that cannot be upgraded. Heck, you might not even know what VB6 is 🙂

That’s exactly why this kind of article rarely ever gets written 🙂

And that’s one of the main reasons I created a blog — so I can post these things for me and other people who are in the same boat as I am.

So let’s get to the main plot…

Why oh why?

Visual Basic 6 (the COM variant) isn’t great for doing graphics. Yes, you can load Bitmap (BMP), JPEG, (static) GIF but that’s about it. You cannot load a PNG image on either Image or PictureBox control. And animated GIFs are also a no-no.

There are other good reasons for thinking outside the box, but for now I’ll focus on loading PNG image in VB6.

So, how do we load PNG in VB6?

In short, you cannot — at least not natively.

But VB.Net can…so I thought, how do I bridge that gap? How do I bring over VB.Net’s technology inside VB6?

And no, it’s not calling a Form in VB.Netvia DLL — I want to be able to embed the image in a VB6 form like an OCX.

If you’ve been this path before, you know what VB.Net doesn’t do OCX like VB6 so we’d have to go the DLL route

Creating the VB.Net library

Here is the basic list of steps I did to create the DLL to host a user control:

  1. Create a DLL project in VB.Net
  2. Add a User Control
  3. Add a Picturebox control
  4. Write a public method in the UserControl to load an image (passing the image path to it)
  5. Use Drawing library to load image “FromFile”

This is readily usable in VB.Net, but to make it visible and usable from VB6, we need to do 2 more things:

  • You have to make the project COM visible
  • You have to make the project Inter-Operable

These 2 options are found in the Project Properties (at least for the Professional Edition).

Make COM Visible
Register for InterOp

If you’re using the Express (Free) version like I do, the Inter-Op option isn’t available (or visible).

You would have to open the VBPROJ file with a text editor (like Notepad) and manually add it there (see image below for reference):

Compiling the VB.Net DLL

Once you’ve done the 2 extra steps, just go ahead and compile the VB.Net DLL as you normally would.

What those extra 2 steps would do is create an additional binary file with a TLB file extension — and that’s the one that we’ll use in VB6.

Creating the VB6 client application

Now at this point you’d be forgiven to think it’s super easy now — we just load the OCX and drag the control to the form, right?

But you know it can’t be that easy 🙂

You can’t find it on the components since it’s not an OCX, so you’ll have to add the TLB file as Reference (the way we reference DLLS).

Once you’ve loaded it, you won’t see any new control so stop looking at the toolbar 🙂

Dynamically Loading the control

The way to do it is by loading the control dynamically (which is also how you’d load custom OCX by the way).

Here’s a very basic code for loading controls dynamically, if you’ve not seen or done it before:

If you’ve read my other posts about dynamic DLL then you’ll see how similar they are.

Okay, let’s apply the same concept to the VB.Net library we created and then run it to see if it works.

It worked!

Here’s the bare minimum code and the output screenshots:

No kidding 🙂

If you’re in front of a computer, you’re probably trying to load a PNG (on the Form, Picturebox or Image) via the LoadPicture function just to check if I’m playing around 🙂

But if you do, you’ll see that it won’t work:

Try it!

Well, just for fun, unless you actually have a project that you’ll need this cool trick 🙂

As for me, I’d go back to my fringe mind 🙂

Leave a Comment