Android Question Clicked Panel in Array

Declan

Well-Known Member
Licensed User
Longtime User
I have an Array of Panels, set as follows:
B4X:
    calPanels = Array As Panel(pnl1,pnl2,pnl3,pnl4,pnl5,pnl6,pnl7,pnl8,pnl9,pnl10,pnl11,pnl12, _
                                pnl13,pnl14,pnl15,pnl16,pnl17,pnl18,pnl19,pnl20,pnl21,pnl22,pnl23, _
                                pnl24,pnl25,pnl26,pnl27,pnl28,pnl29,pnl30,pnl31,pnl32,pnl33,pnl34, _
                                pnl35,pnl36,pnl37,pnl38,pnl39,pnl40,pnl41,pnl42)
I want to identify which Panel received a Click event:
Something like:
B4X:
Sub calPanels()_Click
 

Cableguy

Expert
Licensed User
Longtime User
You have a few options, one is for example, to set each panel tag propertie and in give every panel the same click event, then in the click event, use the sender keyword
like
B4X:
dim p as panel = sender
log("panel " & p.tag & " was clicked")
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Okay, after some thought, I will change the above functionality.
Within each Panel, I have a Label, the text within the represents the day of the month.
So, instead of using the panel_Click event, I will use the label_Click event and read the text from the Label.
Using advice from Cableguy, I have:
Sub Globals:
B4X:
Private calLabels(42) As Label
Sub Activity_Create(FirstTime As Boolean):
B4X:
    calLabels  = Array As Label(lblP1,lblP2,lblP3,lblP4,lblP5,lblP6,lblP7,lblP8,lblP9,lblP10,lblP11,lblP12, _
                                lblP13,lblP14,lblP15,lblP16,lblP17,lblP18,lblP19,lblP20,lblP21,lblP22,lblP23, _
                                lblP24,lblP25,lblP26,lblP27,lblP28,lblP29,lblP30,lblP31,lblP32,lblP33,lblP34, _
                                lblP35,lblP36,lblP37,lblP38,lblP39,lblP40,lblP41,lblP42)
Set Label.Tag:
B4X:
calLabels(pnlNumber).Tag = x '<<---- pnlNumber sets correct position for the Day of Month Text / x == Day of Month (Text)
Click Event:
B4X:
Sub calLabels_Click
    Dim p As Label = Sender
    Log("Label " & p.Tag & " was clicked")
End Sub
The Labels are created in the Designer.
The Click event does not fire?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Did you set the Eventname of all 42 Labels to "calLabels"??? If not; do so.
Like so:
lblP1.Initialize("calLabels")
To
lblP42.Initialize("calLabels")
I tried this initially just to get it working, but Click event is not fired.
Also, all Labels are not populated.
I have attached my project.
 

Attachments

  • CalendarTest.zip
    17.6 KB · Views: 188
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No, if the labels are created in the designer you shouldn't initialize them in code, it won't work. You need to set the event name in the Designer.

I think you can select them all and change all the Event Names at once.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Like so:
lblP1.Initialize("calLabels")
To
lblP42.Initialize("calLabels")
Didn´t you said you have defined them in the Designer?
VIEWS WHICH ARE INSIDE A LAYOUTS SHOULD NOT BE INITIALIZED IN CODE

Again: Set the EventName of ALL 42 lbls in the Designer to "calLabels"!
REMOVE The 42 lines:

' lblP1.Initialize("calLabels")
' lblP2.Initialize("calLabels")
' lblP3.Initialize("calLabels")
' lblP4.Initialize("calLabels")
' lblP5.Initialize("calLabels")
' lblP6.Initialize("calLabels")
' lblP7.Initialize("calLabels")
' lblP8.Initialize("calLabels")
' lblP9.Initialize("calLabels")
' lblP10.Initialize("calLabels")
' lblP11.Initialize("calLabels")
' lblP12.Initialize("calLabels")
' lblP13.Initialize("calLabels")
' lblP14.Initialize("calLabels")
' lblP15.Initialize("calLabels")
' lblP16.Initialize("calLabels")
' lblP17.Initialize("calLabels")
' lblP18.Initialize("calLabels")
' lblP19.Initialize("calLabels")
' lblP20.Initialize("calLabels")
' lblP21.Initialize("calLabels")
' lblP22.Initialize("calLabels")
' lblP23.Initialize("calLabels")
' lblP24.Initialize("calLabels")
' lblP25.Initialize("calLabels")
' lblP26.Initialize("calLabels")
' lblP27.Initialize("calLabels")
' lblP28.Initialize("calLabels")
' lblP29.Initialize("calLabels")
' lblP30.Initialize("calLabels")
' lblP31.Initialize("calLabels")
' lblP32.Initialize("calLabels")
' lblP33.Initialize("calLabels")
' lblP34.Initialize("calLabels")
' lblP35.Initialize("calLabels")
' lblP36.Initialize("calLabels")
' lblP37.Initialize("calLabels")
' lblP38.Initialize("calLabels")
' lblP39.Initialize("calLabels")
' lblP40.Initialize("calLabels")
' lblP41.Initialize("calLabels")
' lblP42.Initialize("calLabels")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I was not aware that you could set EventName in the Designer.
I strongly suggest to read and understand the Beginners Guide.
Reading and knowing the BASICS could have made this thread absolutely not needed.

Thank you for wasting my time!
 
Upvote 0
Top