Looping over members of a Panel

wimpie3

Well-Known Member
Licensed User
Longtime User
I have a Panel with ten labels. I want the labels to change their color to red when they are clicked on. All other labels should change back to blue, their original color.

Seems to me I have to find out the parent panel of the clicked label, and loop over all label members of that panel to change the color, but I have really no idea how to do that... any help here?
 

klaus

Expert
Licensed User
Longtime User
You could use a code like this:
B4X:
Sub Globals
    Dim pnlTest As Panel
    Dim CurrentLabel As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    pnlTest.Initialize("")
    Activity.AddView(pnlTest, 0, 0, 100%x, 100%y)
  
    Dim i, h0, h1 As Int
    h0 = 40dip
    h1 = h0 + 1dip
    For i = 0 To 9
        Dim lbl As Label
        lbl.Initialize("lblTest")
        lbl.Color = Colors.Blue
        lbl.TextColor = Colors.White
        lbl.Tag = i
        lbl.Text = "Test " & i
        pnlTest.AddView(lbl, 0, i * h1, 80%x, h0)
    Next
End Sub

Sub lblTest_Click
    If CurrentLabel.IsInitialized Then
        CurrentLabel.Color = Colors.Blue
    End If
    CurrentLabel = Sender
    CurrentLabel.Color = Colors.Red  
End Sub
Attached the test program.

Best regards.
 

Attachments

  • LabelsInPanel.zip
    6 KB · Views: 134
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
Excellent... so the trick is to keep track of the previous selected label. Thanks.
 
Upvote 0
Top