Label index property

Markob1984

New member
Joined
Sep 8, 2019
Messages
4
Programming Experience
Beginner
hey guys

im trying to convert from vb6 to vb.net 2019 and was wondering if you can help me please.

im wanting to put 20 labels on my form and name them all the same name and use the index property like this

lbltest(1) but i noticed that vb.net dosent allow this anymore can anyone help me do this please?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,522
Location
Lansing, MI; USA
Programming Experience
10+
hey guys

im trying to convert from vb6 to vb.net 2019 and was wondering if you can help me please.

im wanting to put 20 labels on my form and name them all the same name and use the index property like this

lbltest(1) but i noticed that vb.net dosent allow this anymore can anyone help me do this please?
Control arrays in the form designer isn't something .Net has ever supported as it's not needed.
If you really need to do a control array you can add the labels to the form then create a List(Of Label) variable and in the form's Load event set the List to a new instance and populate it with the labels from the form, then all you need to do is use the index of your list like you would in a vb6 control array.

But if you were using a control array in vb6 to easily set their Click event for convenience the .Net way of doing that is to select all the labels on your form (and any other control that has the same event that you want handled by the same sub) and click the "Events" tab in the properties then double click "Click" (or any of the other events you need) and it'll create the Click event sub with all of your selected controls already set in the Handles clause.
 

Markob1984

New member
Joined
Sep 8, 2019
Messages
4
Programming Experience
Beginner
hi thanks for the reply.

would you beable to provide me with some example code if its possible?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,522
Location
Lansing, MI; USA
Programming Experience
10+
I created a form and added 3 labels to it then paste this code:
Code:
Option Explicit On
Option Strict On
Option Infer Off

Friend Class Form1

    Private m_LabelsList As List(Of Label)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        m_LabelsList = New List(Of Label)
        m_LabelsList.AddRange(New Label() {Label1, Label2, Label3})
    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        For Counter As Integer = 0I To m_LabelsList.Count - 1I
            m_LabelsList(Counter).Text = $"Label {(Counter + 1I).ToString("n0")}"
        Next Counter
    End Sub

End Class
 

Markob1984

New member
Joined
Sep 8, 2019
Messages
4
Programming Experience
Beginner
is this an alternative way to using the label index property in vb6?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,127
Location
Sydney, Australia
Programming Experience
10+
As has been suggested, arrays of controls in VB.NET are exactly the same as arrays of anything else. If you could use an array in VB6 then you already know how to use arrays of controls in VB.NET. The specific purpose of dedicated control array support was to allow events of multiple controls to be handled in one place and VB.NET provides a different mechanism for that.

Perhaps you should explain what you're actually trying to achieve, rather than how you're trying to achieve it. Why do you think that it would be useful to be able to use an Index property in the first place? The problem is almost certainly that you're trying to achieve something the wrong way. If we know what that something is, we can tell you the right way. In short, use common event handlers to handle events for multiple controls, use arrays for controls exactly as you would for Integers or Strings and don't use arrays if they don't add actual value.
 

Markob1984

New member
Joined
Sep 8, 2019
Messages
4
Programming Experience
Beginner
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,127
Location
Sydney, Australia
Programming Experience
10+
That's not what I meant. I'm not about to read that to work out for you what you might need to do. What I meant was, what specific functionality are you trying to achieve. If you don't know and you are just blindly following the book then you probably ought to take a steps back and try to understand what the code the book provides is actually achieving and then look for the best way to achieve that in VB.NET. This is a common mistake that people make when trying to move from one language to another. They look for 1:1 translations when what they should be doing is look at the result of the original code and then looking for the best way to achieve that result in the new language. That may look quite different to the original code.
 
Top Bottom