0

I created a List(Of GroupBox)() that were created at runtime. Each groupbox has labels and textboxes. I need to periodically update values in the textboxes of each groupbox. I need access to TxtCool; TxtHeat; and TxtActualTemp (towards the end of my code) How can I access the text field of each of my textboxes in each groupbox after their creation?

 For j As Integer = 0 To Thermostats.Count - 1
        Dim ThermostatName As String
        Dim Model As String
        Dim IPAddress As String

        With Thermostats(j)

            ThermostatName = .ThermostatName
            Model = .Model
            IPAddress = .IPAddress

        End With

        Dim GrpBox As New GroupBox() With {                        
            .Location = New Point(265, 27),
            .Size = New Size(253, 176),
            .Text = ThermostatName,
            .BackColor = Color.White,
            .FlatStyle = BorderStyle.Fixed3D,
            .BackgroundImage = Imagelist1.Images(0),
            .Margin = New Padding(20, 30, 0, -10)
            }

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(146, 65),
            .AutoSize = True,
            .Text = "Heat To",
            .Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(210, 80),
            .AutoSize = True,
            .Text = TempUnit,
            .Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(146, 110),
            .AutoSize = True,
            .Text = "Cool To",
            .Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(210, 125),
            .AutoSize = True,
            .Text = TempUnit,
            .Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(15, 66),
            .Size = New Size(125, 15),
            .Text = "Current Temperature",
            .Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(90, 80),
            .AutoSize = True,
            .Text = TempUnit,
            .Font = New Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold),
            .Visible = True
        })

        GrpBox.Controls.Add(New Label() With {
            .Location = New Point(20, 20),
            .AutoSize = True,
            .Text = Model,
            .Font = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
            .Visible = True
        })

        Dim TxtCool As New TextBox() With {                               
           .Location = New Point(150, 125),
           .Size = New Drawing.Size(60, 25),
           .Visible = True,
           .Enabled = False,
           .BackColor = Color.White,
           .ForeColor = Color.Black,
           .BorderStyle = BorderStyle.FixedSingle,
           .Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
           .Text = 23.0                                                
       }
        GrpBox.Controls.Add(TxtCool)

        Dim TxtHeat As New TextBox() With {                             
            .Location = New Point(150, 80),
            .Size = New Size(60, 25),
            .Visible = True,
            .Enabled = False,
            .BackColor = Color.White,
            .ForeColor = Color.Black,
            .BorderStyle = BorderStyle.FixedSingle,
            .Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
            .Text = 20.5                                                   
        }
        GrpBox.Controls.Add(TxtHeat)

        Dim TxtActualTemp As New TextBox With {                         
            .Location = New Point(19, 82),
            .Size = New Drawing.Size(75, 45),
            .Visible = True,
            .Enabled = False,
            .BackColor = Color.White,
            .ForeColor = Color.Black,
            .BorderStyle = BorderStyle.FixedSingle,
            .Font = New Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold),
            .Text = (6 * Rnd() + 19)                                 
        }
        GrpBox.Controls.Add(TxtActualTemp)
        GrpBoxes.Add(GrpBox)

        Me.TblLytPnl.Controls.Add(GrpBox)

    Next
MeanJean
  • 1
  • 1
  • First, you should enable [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) for this project and set it to be the default for new projects. Then, after fixing the problems it shows you, perhaps adding a `.Name` property for each control would make it easier for you to find it. – Andrew Morton May 04 '20 at 12:59
  • 1
    Instead of creating all those controls in code, design a user control. You can then just create instances of that and all the functionality for the child control sis internal. You can add three properties to your user control that passes through to the `Text` of those `TextBoxes`. – jmcilhinney May 04 '20 at 16:37
  • Thanks jmcilhinney, but the controls depend on the number of thermostat units that are setup by each user. I create as many as needed from an initiation/parameters file. – MeanJean May 05 '20 at 09:57
  • Finally found out how to access child controls myself: `For j = 0 To Thermostats.Count - 1 Dim GroupBox As Control = GrpBoxes.Item(j) Dim ChildActualTemp As Control ChildActualTemp = GroupBox.GetChildAtPoint(LocActualTemp) ChildActualTemp.Text = (6 * Rnd() + 19) Next` But you need to know the exact location of your child control within the GroupBox. – MeanJean May 05 '20 at 09:58

0 Answers0