Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of controls in VB.net 1

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
If I have a number of labels that will be used as controls,
how do I set them as an array in VB.net(label1(0),Label1(2), etc)?
This is easily done in VB6. Clicking on a label then can be used by reading it's index.
I'd appreciate some coding as an example.
Thanks for any help.
 
There are no special control arrays in .Net. Arrays of controls are treated just like arrays of anything else. Here is an example to load up ten textboxes in an array on a form.

Code:
Public Class Form2
    Dim SomeTextBoxes(-1) As TextBox
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 10
            ReDim Preserve SomeTextBoxes(i)
            SomeTextBoxes(i) = New TextBox
            Me.Controls.Add(SomeTextBoxes(i))
            SomeTextBoxes(i).Left = 4
            SomeTextBoxes(i).Top = (24 * i) + 4
        Next
    End Sub
End Class
 
Thanks RiverGuy
I copied your code and it worked for text boxes.
But when I substituted Labels for text boxes it didn't.
Here is the code I tried to run. Where did I go wrong?

Public Class Form1
Dim SomeLabels(-1) As Label
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
ReDim Preserve SomeLabels(i)
SomeLabels((i) = New Label(i)
Me.Controls.Add(SomeLabels((i))
SomeLabels((i).Left = 4
SomeLabels((i).Top = (24 * i) + 4
Next
End Sub
End Class
 
See the difference between these two lines...

[tt]
SomeLabels((i) = New Label(i)
SomeTextBoxes(i) = New TextBox
[/tt]



Good Luck
 
VB5prgrmr, Here is my coding, followed by the error messages
I can't see what's lacking or is wrong.
Thanks for your interest.
Public Class Form1
Dim SomeTextBoxes(-1) As TextBox
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
ReDim Preserve SomeTextBoxes(i)
SomeTextBoxes(i) = New TextBox
Me.Controls.Add(SomeTextBoxes(i))
SomeTextBoxes(i).Left = 4
SomeTextBoxes(i).Top = (24 * i) + 4
Next
End Sub
End Class

Error 1 Expression is not a method. 6 13
Error 2 Too many arguments to 'Public Sub New()' 6 40
Error 3 Expression is not a method. 8 13
Error 4 'Left' is not a member of 'Integer' 8 24
Error 5 Expression is not a method 9 13
Error 6 'Top' is not a member of 'Integer' 9 24
 
[tt]
SomeLabels((i) = New Label(i)
SomeTextBoxes(i) = New TextBox
[/tt]
 
Sorry about my last message. The code should have used Labels, not Text Boxes.

Anyway, reading through VB.net "Help" regarding arrays, it seems the VB6 capability of creating a number of labels on the form as arrays, and using them as controls utilizing the array index is not supported in Vb.net.
So, unless someone can suggest something that emulates VB6 in this regard, I'll have to think of another approach.
I thank all responders to my question.
 
As long as you are using framework 2.0 or > then:

'class declaration, type specific collection/no need to cast the type
Dim myLabels As New List(Of Label)

For i As Integer = 1 To 10
Dim l As New Label
'add stuff to properties
myLabels.Add(l)
Next

'index access
myLabels(0).Text = "something"

'easy remove options
myLabels.RemoveAt(0)
myLabels.RemoveAll()
 
Thank you VB4Life
I ran the following version of your code. Result, a blank form with no error list. What is framework 2, and how do I know I have it?
As long as you are using framework 2.0 or > then:

'class declaration, type specific collection/no need to cast the type
Dim myLabels As New List(Of Label)

For i As Integer = 1 To 10
Dim l As New Label
'add stuff to properties
myLabels.Add(l)
Next

'index access
myLabels(0).Text = "something"

'easy remove options
myLabels.RemoveAt(0)
myLabels.RemoveAll()
 
The framework is your version of VS. 2008 is FW 3.5, 2005 is FW 2.0. Meaning as long as you are using VS2005 or later the Generics List(Of T) is available for you. This is a great new class that in my mind replaces ArrayList - it helps avoid casting errors as it is a type specific collection.
 
Thanks again VB4Life, but can you see from the code I showed you why it didn't work?
 
Do you mean the code you have from earlier that does not work? That's why i posted a different solution - one that works better than an array. Do you need more info on how it works?
 
VB4Life said:
Do you mean the code you have from earlier that does not work? That's why i posted a different solution - one that works better than an array. Do you need more info on how it works?

I'm curious -- in what way does a generic list work better than an array?
 
Easier to add/remove collection items, in my opinion. The code looks cleaner and makes more sense - to me. So I guess I should have said IMHO. Wasn't trying to offend.
 
VB4Life, No I mean the code you sent me, as shown in my response to you, didn't work.
It produces a blank form and no error list.
 
Just rereading your original post, are you adding labels that already exist on your form, or are you need to dynamically add them? If you are just adding a group of existing labels you could do this:
Code:
'make the collection
Dim lbs As New List(Of Label)

'add the labels
lbs.AddRange(New Label() {label1, label2, label3})

'make the event handler for each label in collection
For Each l As Label In lbs
   AddHandler l.Click, AddressOf label_Click
Next

'click event, sender is the label being clicked on
Private Sub label_Click(ByVal sender As Object, ByVal e As EventArgs)
  Debug.WriteLine(DirectCast(sender, Label).Text)
End Sub
 
Note with using the last code you posted BadCook they don't show up on the form because that code never puts them on the form. I think what really needs to be asked is what are you trying to get out of this? If you are just trying to group them for use then you can simply add them to the from and then add them to a List(Of like the first part of what VB4Life did there. Then to it would be just as easy to put them on their own panel and then use the panels control array to access them. On the Label Panel set the Label Cool to "Cool".
Code:
LabelPanel.Controls("lblCool").Text = "Cool"
A little better would be to Type Cast it:
Code:
Ctype(LabelPanel.Controls("lblCool"), Label).Text = "Cool"
Even better would be to make sure that it exists and is a label.
Code:
If LablePanel.Controls("lblCool") IsNot Nothing AndAlso TypeOf LablePanel.Controls("lblCool") Is Label Then
    CType(LablePanel.Controls("lblCool"), Label).Text = "Cool"
End If


That all assumes they already exist on the form. Maybe you want a control that always has 2 labels and only 2 labels.
Code:
Public Class LabelGroup
    Inherits Windows.Forms.Control

    Friend WithEvents LabelCool As Windows.Forms.Label
    Friend WithEvents LabelReallyCool As Windows.Forms.Label

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Me.LabelCool = New Windows.Forms.Label
        Me.LabelReallyCool = New Windows.Forms.Label
        Me.SuspendLayout()

        Me.LabelCool.Name = "LabelCool"
        Me.LabelCool.Location = New System.Drawing.Point(10, 10)
        Me.LabelCool.Size = New System.Drawing.Size(100, 13)
        Me.LabelCool.Text = "Add Text"

        Me.LabelReallyCool.Name = "LabelReallyCool"
        Me.LabelReallyCool.Location = New System.Drawing.Point(10, 33)
        Me.LabelReallyCool.Size = New System.Drawing.Size(100, 13)
        Me.LabelReallyCool.Text = "Add Text"

        Me.Controls.Add(LabelCool)
        Me.Controls.Add(LabelReallyCool)
        Me.Name = "LabelGroup1"
        Me.Size = New System.Drawing.Size(150, 66)
        Me.ResumeLayout()
    End Sub
End Class
And use it like this.
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim CoolGroup As New LabelGroup
        CoolGroup.LabelCool.Text = "Cool"
        CoolGroup.LabelReallyCool.Text = "Really Cool"
        CoolGroup.Location = New Point(10, 10)
        Me.Controls.Add(CoolGroup)
    End Sub
As you can see the first question is really what are you trying to do?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I think my main problem in all this is my failure to communicate properly.
Lets try this:
I'm sure most of you have used VB6 before moving to VB.net as I'm trying to do. Here is a sample of what I want, in VB6.
I place a label on a form, copy it to the form, am asked if I want an array, say yes, then paste two more labels on the form though I could have added a hundred or more. I also add another label, Label2. That's all.
The code for this is:

Private Sub Label1_Click(Index As Integer)
Label2.Caption = Index
Beep
End Sub

HOW DO I DO THIS IN VB.NET?
 
I place a label on a form, copy it to the form, am asked if I want an array, say yes, then paste two more labels on the form though I could have added a hundred or more. I also add another label, Label2. That's all.

You cannot use the GUI to work with arrays of controls in VB.Net. There is no similar functionality to VB6 in this regard.

We demonstrated that you can have arrays and collections of objects -- including controls -- in VB.Net. But if this doesn't help you with your task, you are better off creating individual controls on your form. You can still iterate through the container's controls collection in your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top