Changing Label.Backcolor in a Control Array

laredo512

Regular
Joined
Jan 6, 2004
Messages
88
Location
Far enough to see snow in winter
Hi,

Got this problem trying to change the BackColor of multiple lables.

Using VB.NET pro 2003

Code:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] chgCol([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] LabelNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] lblMan [/size][size=2][color=#0000ff]As[/color][/size][size=2] Label[/size]
[size=2]lblMan = FindControl("lblman" & LabelNumber.ToString)[/size]
[size=2][color=#0000ff]If[/color][/size][size=2] lblMan.Text [/size][size=2][color=#0000ff]Is[/color][/size][size=2][color=#0000ff]Nothing[/color][/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]lblMan.BackColor = System.Drawing.SystemColors.Control[/size]
[size=2][color=#0000ff]Else[/color][/size]
[size=2]lblMan.BackColor = System.Drawing.Color.Green[/size]
[size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size]
 
Private Function FindControl(ByVal Controlname As String) As Label 
 
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Label Then
If ctl.Name.ToUpper = Controlname.ToUpper Then
Return ctl
End If
End If
Next
 
End Function

I keep getting a reference not set to an instance of an object error....

I'm sure it's something stupid, but I can't put my finger on it.

Thanks for any help provided!
 
I found the issue and solved it with this:

Code:
[size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] chgCol([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] Label)

[/size][size=2][color=#0000ff]If[/color][/size][size=2] e.Text = "" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]e.BackColor = System.Drawing.SystemColors.Control

[/size][size=2][color=#0000ff]Else

[/color][/size][size=2]e.BackColor = System.Drawing.Color.Green

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] lblMan1_TextChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] lblMan1.TextChanged

chgCol([/size][size=2][color=#0000ff]Me[/color][/size][size=2].lblMan1)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff]
[/color][/size]

 
Back
Top