Map Generator in VB6

It’s quite rare to encounter programs written in VB6 that can generate a dynamic number of controls on the fly. The primary reason is that there’s rarely any business or industry that uses VB6 which would require it. One industry would be gaming, but a more common one is semiconductor.

Although creating a control dynamically and from scratch in VB6 is nothing new and simple enough to do, the problem with that approach is that interacting with those created-on-the-fly controls is not that simple.
It’s possible, but requires a lot more effort compared to just creating an array of it from the design and just multiply it during runtime. This approach allows us to easily access the “shared” events across all the created controls very easily.

Proof Of Concept

To illustrate, I created a demo program that generates a sort of strip map (with random pre-selected colors) as indicated by the number of rows and columns (representing the strip dimension).

The individual “bins” (represented by each number) is also clickable, and I’ve simply echoed the information associated with that control

Major Form Controls

Not including the obvious controls and only the core widgets, this super simplified version has 3 picture boxes (the 1st is for the canvas that represents the strip, the 2nd is the viewable area and the 3rd is the corner widget [shown when both scroll bars are visible]).

Then it has 1 horizontal scroll bar (initially hidden), 1 vertical scroll bar (initially hidden).

The bin is a label. Why label? Cause we need something flat and lightweight. You can use textbox or command button, but those have more events and properties and I consider them “heavyweight” controls, and in this case I don’t need those additional features — I just need something that can have text and can be clicked, so label is enough.
The main thing you need to set on this label properties is “Index” which defaults to blank. You type in “0” (zero) to indicate that it’s an array control — this is critical, as it enables us to generate more of this widget at runtime.
You must also set this label to hidden (Visible = False) so it only shows when we generate the map.

Dynamic Bin (Label) Creation

The dynamic label is creation is super simple. You just call the “Load” method which creates a new instance of a control. In this case, it would look something like this:

index = index + 1
load lblBin(index)

The other part you need to do is figure out the coordinates of the newly created label. For that, I simply set a fixed size (800 px) that would represent a bin and multiplied it with both the row and column index to get its position.

xpos = col_index * sprite_size
ypos = row_index * sprite_size

lblBin(index).move xpos, ypos

Scrolling Behavior

I’ve also added scrollbars that behaves according to the content. For example, only the vertical scroll bar appears when the strip is taller than the viewable area:

It also only shows the horizontal scroll bar if the strip is wider than the viewable area:

And both scroll bars appear when the strip is both wider and taller than the viewable area:

Most VB6 programmers do this manually, meaning they literally do this on design and manually places each control on the form (or picture box), that’s why most of them complain when the number of controls change or is required to be dynamic.

If you know this technique I’ve shown here, that wouldn’t be a problem to code, though if the number becomes too big, as usual, do evaluate the resource consumption and maybe consider “drawing” instead of generating controls. I know it’s hard to do in VB6, but in VB.Net it’s easy and I might write an article on that too to complement this one 🙂

Here’s the link to my GitHub page for the complete source code:

https://github.com/vegitz/codes/tree/master/0005%20map%20generator%20vb6

Thank you and see you on the next article! 🙂

Leave a Comment