Click to See Complete Forum and Search --> : Problem with a Sub's Argument


JDP
February 18th, 2003, 03:09 AM
I have a Form with a number of Panels and each panel contains a large number of labels and pictureboxes.

When the user clicks on any label, I want the text forecolor of all the labels in that particular panel to change to another color .

I thought I could do it with this Sub, but it throws an error :-



Private Sub ChangePanelTextColor(ByRef PN As Panel)
Try
Dim l As Label
For Each l In PN.Controls
l.ForeColor = System.Drawing.Color.BurlyWood
Next

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End Sub


and I called it from a Label_Click event in the usual way


ChangePanelTextColor(<Name of Panel containing this Label>)

Have I missed something really obvious and basic here ? Or do I have to go about it in a completely different way?

JDP

DdH
February 18th, 2003, 06:27 AM
Your problem happens when the panel also contains other controls then labels.
Then the For Each tries to fill 'l' with an different type of control then a label.


Try the following sub

Private Sub ChangePanelTextColor(ByRef PN As Panel)
Try
Dim l As Control
For Each l In PN.Controls
If l.GetType Is GetType(Label) Then
l.ForeColor = System.Drawing.Color.BurlyWood
End If
Next

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End Sub

JDP
February 18th, 2003, 01:41 PM
Many thanks, Jan.

That did the trick!

JD