Reputation: 1
Of course I've searched through all types of forums relating to the "NullReferenceException" error and they all say that my objects = nothing and that I need to use the "new' operand to make it work. I'm building my Form dynamically using groupboxes and then inserting labels and textboxes in each groupbox. Some textboxes will need to be regularly updated but I don't want to update the whole form unnecessarily since it makes the Form flicker when updating(I've tried). I've was able to build my form normally using the "new groupbox" function but was unable to access objects in each of the groupboxes once it was built. Maybe it has something to do with the fact that I didn't make an indexed array out of the groupboxes. Rebuilding the whole Form was my only way to refresh he displayed values, but like I stated, it makes the the Form flicker. So I tried to make an indexed array out of the Groupboxes, this is where I get the "NullRefereceException" error. I've already declare my Groupbox array.
Public Shared GrpBox as GroupBox()
Here is my code at this point:
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
GrpBox(j) = 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(j).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(j).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(j).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(j).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(j).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(j).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(j).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(j).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(j).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(j).Controls.Add(TxtActualTemp)
Me.TblLytPnl.Controls.Add(GrpBox(j))
Next
Upvotes: 0
Views: 48