No control arrays in Visual Basic 2005 (resolved)

2,348
murphykieran
I recently downloaded Visual Basic 2005 Express and I'm trying to reteach myself the basics of Visual Basic.

I had to learn Visual Basic while at college a couple of years ago. It was Visual Basic 6 we used. I remember being able to create an array of controls on a form so that they could by cycled through by using a For-Next loop and simply referring to their index number.

I could have an array of labels and if I wished to cycle through them and put a particular piece of text from a variable array I could just create a loop and stick a line of code like the following into it:
Code:
myLabel(x).Text = myArray(x)
It was easy because I could create a simple one-to-one correspondence between an array of controls and an array of variables.

But I can't do that in Visual Basic 2005 because the ability to create control arrays has been removed and I can't figure out how to do what I want to do.

The program I'm trying to write involves clicking a button which will generate a particular set of numbers in an array and I wish to then display them in a set of labels on the form. The idea of having to refer to each individual label in the code so that I can do something to it instead of being able to simply loop through an array seems ugly and messy.

I assume there's a quick and easy method of doing something similar in Visual Basic 2005, but I can't figure out from the help file what it is. Can anyone give me some help as to how I'd go about doing what I want to do? I hope my request is clear.


KM.
 
To do that, you cannot use the built-in designer. Unfortunately, I don't know VB.NET very well, but I'll give you response with C# snippets and you can convert (I hope).

The best way is to put all the labels on a panel; I'll call it panel1 and assume you've added that in the designer. In the constructor for Form 1, you need to add the Labels manually after the call to InitializeComponent(). The steps are as follows:
Code:
for (int i=0; i<ARRAY_MAX; i++) {
    //create the label object
    Label tmp = new Label();
    //set it's position
    tmp.Location = new Location(0,0);
    tmp.Size = new Size(50,100);
    //add it to the panel
    panel1.Controls.Add(tmp);
    //place it in the array
    myArray[i] = tmp;
}
But as you can see the downside is you have to manually position it. In the code snippet above, all the Labels are goin to lay out on top of each other--not very pretty. As you iterate through the array, you probably want to change the location based on i, such as:
Code:
    tmp.Location = new Location(0,i*50);
This will lay them out one below the other. You can tweak it to get them to lay out the way out want.

HTH, and feel free to ask if my C# snippets aren't clear enough to a VB developer! :)
 
If you want to place the controls on the form at design time (I love that form designer), you could also put the labels onto a panel, and then use its controls property, which returns a collection of its child controls. You to access any of them by using its name as the index.

PHP:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim nbItems as Integer = 2

        For i = 1 To nbItems
            Panel1.Controls("label" & i).Text = "This is label #" & i
        Next
End Sub

All control containers in vb.net have that property, including forms, so you could also do it outside of a panel.

Hope that helps.
 
Thanks! :)

On a side note, It's really nice how close vb.net and c# have come, jumping from one to another is now something done so easily, even more so with VS.Net 2005.

I'm an absolute novice in C++ though, only used it in a few classes at school... :indiff:
 
I'm at the very beginning of learning C# at the moment. I did learn a bit of Visual Basic 2003 a year or so back, and they are both quite similar.

I just can't understand strings, booleans, doubles, intergers, etc yet. :embarrassed: Hey, I've only been learning it for 4 days. :lol:
 
Okay, I seem to have it solved now using Carl's method. I didn't realise you could refer to a control called something like Label1 by using a construct like
Code:
"Label" & i
where i was a variable with the value 1. I thought you had to refer to the exact name in the code.

Anyway, it's working now. Thanks all for the help!

edit: According to the sticky at the top of the forum, I should add a "(resolved)" note to the thread title, but I can't find the option anywhere.

edit2: What I had to do was edit the opening post in advanced mode and change the title.


KM.
 
Back