Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Old school stuff, but I'm old.
I've watched numerous videos and read various techniques of creating objects dynamically (control arrays), most of the techniques involve a Button_Click Sub that creates one object per click. I'm creating the multiple objects at Form_Load, all goes good with this part.
None of these above mentioned instructions or tutorials go to the next step of how to address these newly created objects to change or update properties (i.e., text, colors) to indicate updates/changes in the program's other processes, including BackgroundWorker.
I cannot figure out how to address these new objects.....what is their 'name' variable in the array in the sample code?
I've tried to keep this sample very simple, this is not my 'homework'. Just appreciate some guidance on how these objects are addressed so I can move on with this project to save mankind. :)
Thanks!

What I have tried:

VB.NET
Public Class Form1
    Public iUnits As Integer = 5
    'Dim lbl(0) As Label
    Dim lbl(0) As Control

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateLabels()

    End Sub

    Public Sub CreateLabels()
        Dim X As Integer = 10 'starting left in GroupBox
        Dim Y As Integer = 20 'starting down in GroupBox
        Dim i As Integer

        For i = 0 To iUnits - 1
            Dim lbl As New Label

            With lbl
                .Size = New Size(100, 25)
                .Location = New Point(X, Y)
                .Name = i + 1
                .Font = New Font("Calibri", 10, FontStyle.Bold)
                .BackColor = Color.SkyBlue
                .TextAlign = ContentAlignment.MiddleCenter
                .Text = "Core " & i + 1
                .Tag = i
            End With
            Me.GB_Table.Controls.Add(lbl)

            Y = Y + 30
        Next

        GB_Table.Height = Y + 10
        GB_Table.Width = X + 100 + 10
        GB_Table.Text = "Core Temp "
        '*********
    End Sub

    Private Sub btn_process_Click(sender As Object, e As EventArgs) _
                 Handles btn_run.Click
        ' here I want to address a specific Label (ex: lbl 3)and change/update Text, Color, etc properties
        ' but I can't seem to 'find' it. 
        'I do NOT want a For All or For Each-Next loop

    End Sub

    Private Sub btn_close_Click(sender As Object, e As EventArgs) _
           Handles btn_close.Click
        Me.Close()

    End Sub
End Class
Posted
Updated 27-Oct-23 7:15am
v2
Comments
Member 15627495 26-Oct-23 2:33am    
Me.GB_Table.Controls(i)
Me.GB_Table.Controls(0)
Me.GB_Table.Controls(250)

in Forms, and Controls, the Controls Array is made by all Componants in.

there are some Controls which need a depth more level to be reached, but you have understand how already.


if typeof Me.GB_Table.Controls(0) is Label then
 dim temp_ctrl_label as Label = Me.GB_Table.Controls(0)
 temp_ctrl_label.Text = " Surprise !"
 
end if
OriginalGriff 26-Oct-23 4:43am    
Answer updated.

1 solution

They don't have "name variables" because they are created at run time (when your app is executing) rather than at compile time - so the only time anyone knows how many of them there are is long after all the variable names have been assigned.
So you can't later access them "by name" as that code needs to know now many there are when you compile it!

That's not a major problem though, as you don't need to know the name of something to access it: if you have a Children class, and you create a Family as an array:
VB
Dim Joe As Child = New Child()
Joe.name = "Joe"
Dim Sarah As Child = New Child()
Sarah.name = "Sarah"
Dim Pete As Child = New Child()
Pete.name = "Pete"
Dim family As List(Of Child) = New List(Of Child)()
family.Add(Joe)
family.Add(Sarah)
family.Add(Pete)
Then you could access the girl via her name:
VB
Console.WriteLine(Sarah.name)
Or by accessing the Family:
VB
Console.WriteLine(family(1))
With an array of Controls, you can do the same thing:
VB
For Each ctrl As Control In Me.GB_Table.Controls
    If TypeOf ctrl Is Button Then
        ...
    End If
    Console.WriteLine(ctrl.Text)
Next
Make sense?

Quote:
Interesting, and I'll try several strategies here.
The concept of 'Child' would seem that it needs to be done at coding time BUT the first sentence 'They don't have "name variables" because they are created at run time (when your app is executing)' makes me wonder if it would be easier to code one Label object at coding time, and then create the additional objects at RunTime?
I would always have at least 1 but could have as many as 10-20 as a txt files is read in the initial Loading operation. Does that make sense?


Yes, and it's the same thing: I was just using "named variables" as an example to show you in code you might be familiar with.
VB
For i = 0  to N
   Dim lbl As New Label
   lbl.Text = i.ToString()
   Me.GB_Table.Controls.Add(lbl)
End For
...
For Each ctrl As Control In Me.GB_Table.Controls
    If TypeOf ctrl Is LabelThen
            Console.WriteLine(ctrl.Text)
    End If
Next
lbl only exists inside the first loop, but all the Labels are available in the second.
 
Share this answer
 
v2
Comments
OriginalGriff 26-Oct-23 4:43am    
Answer updated.
Maciej Los 28-Oct-23 13:15pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900