Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Simulating control arrays in eVB

Written by Robert Levy  [author's bio]  [read 39087 times]
Edited by Derek

Download the code

Page 1  Page 2 

One of many features left out of eVB is the ability to create control arrays... or so say experienced programmers on message boards and newsgroups all over the net - because trial-and-error (with the concurrence of MSDN, of course) have dictated such. Well, I'm here to dissent. Anyone who has previously been forced to write a slow, albeit functional, piece of code (see Listing 1) to simulate the behavior of a control array will want to duck to avoid hitting themselves in the forehead with a resounding, "Why didn't I think of that?" Don't feel bad though, it didn't come to me until after three Mountain Dews, a four slices of cold pizza, and a late night visit from the Coding Fairy who replaced the Palm Pilot left under my pillow with the information I'm about to share with you. Besides, the five minutes you spend reading this article will change your life forever - your code will become more efficient, your applications will earn you millions of dollars, and you will be plagued by a harem of beautiful women (or men - your choice) who follow wherever you go in awe of your mastery of eVB control arrays.

Ok, so maybe I was lying about the money and the harem - but not about the efficiency of real control arrays. Before getting into the technical details, lets review some terminology (experienced VB6 programmers may want to skip this paragraph).

First off, here is Microsoft's definition of control arrays: "A control array is a group of controls that share the same name and type ... each control is referred to with the syntax object(index)". For more on control arrays in VB6, see this article.

One of the most popular features of control arrays is the ability to use the "For Each...Next" statement. Again, here is a Microsoft definition: "This statement repeats a block of statements for each element in an array or collection." The method I am about to share with you makes use of "references". Simply put, a reference is just an extra name for an existing variable. If X is a variable and Y is a reference to X, then any operations performed on X are also performed on Y (and vice versa) since X and Y are in fact two different names used for the same place in the computers memory. For more information on this, you'll want to look up the "Set" statement in MSDN's VB6 documentation.

Now let me give you the bad news: there is some code overhead in setting this up, but on the bright side it's mostly cut-and-pasting and once the initial set up is done, you're good to go with an efficient control array.

I was in the midst of creating a game for my iPaq which contained a "game board" of 52 Label controls, when I realized that I needed an easy way to read and modify certain properties of each of these at the same time. Naturally my thoughts turned to using a "For Each...Next" loop to operate on a control array containing all of the 52 labels. That's when I found that the eVB IDE did not provide a way to define such a control array. After the method in Listing 1 gave me unacceptably slow results (although I'm sure it would suffice in situations with far fewer than the 52+ controls I was using), I did some deep thinking. By deep thinking, I mean that I clench my fists, draw eyes on my index fingers, and let my left and right hands converse with each other:

Left: "Darn! I really wish I could use control arrays to solve this problem since that's how it would be done in VB6."
Right: "Yeah, this sucks. Heck, eVB sucks entirely. I quit!"
Left: "Oh, come on now... it isn't that bad... eVB does support regular arrays and For Each...Next loops."
Right: "Bah, what on earth could I possibly need a regular array for?"
Left: "Well, you can use a regular array to hold a series of variables of the same data type... integers, strings, references, etc."
Right: "References? Did you say references?"
Left: "If you accept the idea of me, a clenched fist with hand drawn eyes, being able to speak, then yes - I did in fact say that a regular array could hold references."
Right: "Can a regular array hold references to controls on a form?"
Left: "Of course!"
Right: "Then we could just create an array of control references that would act just like a VB6 control array! Maybe this eVB thing isn't so bad after all!"

The New Way

Given that my hands are still busy talking to each other and I'm thus forced to type this with my feet, I'll keep the rest short and to the point with a simple step-by-step instruction list. I highly recommend you take a look at the well-commented example code provided.

- Place a series of controls on a form (I'm using TextBox controls in this example)

- Give them similar names with identifying numbers at the end (like Text0, Text1, Text2, Text3 - remember to start at 0). These identifying numbers will serve as an equivalent to the Index property available in VB6.

Next Page