CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003
    Location
    Newcastle, UK
    Posts
    2

    Problem with a Sub's Argument

    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 :-

    Code:
    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

    Code:
    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

  2. #2
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    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

  3. #3
    Join Date
    Feb 2003
    Location
    Newcastle, UK
    Posts
    2
    Many thanks, Jan.

    That did the trick!

    JD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured