Sender !!

skipsy

Member
Licensed User
Longtime User
Hi all,

Iam afraid I misunderstood something regarding 'Sender' !

Can someone tells what's wrong with this :

I am trying to locate the selected cell
B4X:
Sub Cell_Cpte_click
   Dim l As Label

   l = Sender
   ToastMessageShow(l.tag,False)
End Sub

At the "ToastMessageShow(l.tag,False)" line I get the error :
"Object should first be initialized (label)"

So I have tried this :
B4X:
Sub Cell_Cpte_click
   Dim l As Label

   l.initialize("")

   l = Sender
   ToastMessageShow(l.tag,False)
End Sub

... Same error.

:BangHead:
Thks,
WW
 

skipsy

Member
Licensed User
Longtime User
Sorry,
Here are some more info.
This is based on a source you wrote about scrollviews :
B4X:
sub blabla
Dim l As Label
Dim CURSOR1 As Cursor
Dim TABLE_CPTE As Panel   
...
....
For i = 0 To CURSOR1.RowCount -1
   CURSOR1.Position = i
   l.Initialize("Cell_Cpte")
   l.Tag = i                                                        ' I guess l.tag keep the ligne number
   l.Text = CURSOR1.GetString( "nomcompte" )
   TABLE_CPTE.AddView( l, 5dip, LF * i, 70dip, LF- 2dip)
Next
end sub

'Now if I click on a label, I want to get its line
Sub Cell_Cpte_click
    Dim l As Label

    l = Sender
    ToastMessageShow(l.tag,False)
End Sub

Thank you,
WW
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must dim each label before adding it to the panel like this:
B4X:
For i = 0 To CURSOR1.RowCount -1
    CURSOR1.Position = i
    Dim l as Label
    l.Initialize("Cell_Cpte")
    l.Tag = i                              ' I guess l.tag keep the ligne number
    l.Text = CURSOR1.GetString( "nomcompte" )
    TABLE_CPTE.AddView( l, 5dip, LF * i, 70dip, LF- 2dip)
Next
Best regards.
 
Upvote 0

voiD

Member
Licensed User
Longtime User
I hope i can explain it right,

so with "Sender" you declare what happens if you click the label, the text is changed, or other events.


I got a simple code to test it, its a little bit messy because iam at school right now

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim btn1, btn2, btn3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   btn1.Initialize("Btn")
   btn2.Initialize("Btn")
   btn3.Initialize("Btn")
   Activity.AddView(btn1,10dip,10dip,100dip,100dip)
   Activity.AddView(btn2,80dip,80dip,100dip,100dip)
   Activity.AddView(btn3,200dip,200dip,100dip,100dip)
   
   
   btn1.Text =  "BTN1"
   btn2.Text =  "BTN2"
   btn3.Text = "BTN3"
End Sub

Sub Btn_Click
   If Sender = btn1 Then
      Msgbox("btn1 was clicked", "Clicked a button")
   Else If Sender = btn2 Then
      Msgbox("btn2 was clicked","clicked a button")
   Else 
      Msgbox("btn3 was clicked", "clicked a button")
   End If
End Sub

So if you add views by code you have to initialize them with the sub name in the brackets that handle the events for the view.
The Sample code is self explanatory, but its a little bit messy ;D

Bad english i know ;D

best regards,

void
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
Thank you VoId, This is matching with what I understood... !

Klaus, I have also tried to add the DIM in the loop.
Hum... I guess the full code could help to see what's wrong :
B4X:
Sub Globals
   Dim SQL1 As SQL

   Dim Pnl_MAJ_Cpte As Panel   
   Dim Bt_Cpt_Ajout, Bt_Cpt_Efface, Bt_Cpt_Sortie As Button
   Dim SV_Comptes As ScrollView
   Dim TABLE_CPTE As Panel

End Sub

Sub Activity_Create(FirstTime As Boolean)

    SQL1.Initialize( "/mnt/sdcard", "bar.db", True)

   Activity.LoadLayout("maj_comptes.bal")
   
   SV_Comptes.Panel.Color=Colors.black
   TABLE_CPTE.Initialize("")
   TABLE_CPTE.Color = Colors.Green



'--------------------------------------------------------------
   Dim CURSOR1 As Cursor
   Dim LIST_CPTE As List
   Dim TXT_SIZE As Int
   Dim LF As Int       

   LIST_CPTE.Initialize
   TXT_SIZE = 20
   LF = TXT_SIZE + 10dip
'      
    CURSOR1 = SQL1.ExecQuery("SELECT nucompte, nomcompte FROM comptes")

   SV_Comptes.Panel.Height = CURSOR1.RowCount * LF   
   SV_Comptes.Panel.AddView( TABLE_CPTE, 10dip, 10dip, SV_Comptes.Width-10, CURSOR1.RowCount * LF )   
   
   For i = 0 To CURSOR1.RowCount -1   
      CURSOR1.Position = i
      LIST_CPTE.Clear
      LIST_CPTE.Add(CURSOR1.GetInt( "nucompte" ) )
      LIST_CPTE.Add(CURSOR1.GetString( "nomcompte" ) )

      Dim l As Label
      l.Initialize("Cell_Cpte")
      l.Gravity = Gravity.CENTER
      l.TextSize = TXT_SIZE
      l.TextColor = Colors.black
      l.Color = Colors.white
      l.Tag = i
      l.Text = LIST_CPTE.Get(0)

      TABLE_CPTE.AddView( l, 5dip, LF * i, 70dip, LF- 2dip)
         
      Dim l As Label
      l.Initialize("Cell_Cpte")
      l.Gravity = Gravity.CENTER
      l.TextSize = TXT_SIZE
      l.TextColor = Colors.black
      l.Color = Colors.white
      l.Tag = i
      l.Text = LIST_CPTE.Get(1)

      TABLE_CPTE.AddView( l, 72dip, LF * i, SV_Comptes.Width-70dip, LF-2dip)
   
   Next


End Sub

Sub Cell_Cpte_click
   Dim rc As RowCol
   Dim l As Label
   
   l = Sender
   ToastMessageShow(l.text,False)

End Sub

Best regards,*
William.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code below works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim i As Int
    SV_Comptes.Initialize(100)
    Activity.AddView(SV_Comptes, 0, 0, 100%x, 100%y)
    
'    For i = 0 To CURSOR1.RowCount -1    
    For i = 0 To 20
'        CURSOR1.Position = i
'        LIST_CPTE.Clear
'        LIST_CPTE.Add(CURSOR1.GetInt( "nucompte" ) )
'        LIST_CPTE.Add(CURSOR1.GetString( "nomcompte" ) )
        Dim l As Label
        Dim rc As RowCol
        rc.Initialize
        rc.row = i
        rc.col = 0
        l.Initialize("Cell_Cpte")
        l.Gravity = Gravity.CENTER
        l.TextSize = TXT_SIZE
        l.TextColor = Colors.black
        l.Color = Colors.white
        l.Tag = rc
'        l.Text = CURSOR1.GetInt("nucompte")
        l.Text = "Test " & i & " 0"

        SV_Comptes.Panel.AddView( l, 0, LF * i, 70dip, LF- 2dip)
            
        Dim l As Label
        Dim rc As RowCol
        rc.Initialize
        rc.row = i
        rc.col = 1
        l.Initialize("Cell_Cpte")
        l.Gravity = Gravity.CENTER
        l.TextSize = TXT_SIZE
        l.TextColor = Colors.black
        l.Color = Colors.white
        l.Tag = rc
'        l.Text = CURSOR1.GetString("nomcompte")
        l.Text = "Test " & i & " 1"

        SV_Comptes.Panel.AddView( l, 72dip, LF * i, SV_Comptes.Width-70dip, LF-2dip)
    Next
    SV_Comptes.Panel.Height = LF * i
End Sub

Sub Cell_Cpte_click
    Dim rc As RowCol
    Dim l As Label

    l = Sender
    rc = l.Tag
    ToastMessageShow(l.text & " / row = " & rc.row & " / col = " & rc.col,False)

End Sub
Attached the test program.
You don't need the LIST_CPTE list.
You should add the labels onto SV_Comptes.Panel and not onto a separate panel.

Best regards.
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
With a breakpoint in
B4X:
sub Cell_Cpte_click
I have noticed that 'Sender' is Null

This is probably why
B4X:
    Dim l As Label

    l = Sender
So 'l' is not initialized.

:BangHead:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Remove the breakpoint and it works !
I tested the program before posting it.

Testing one more, I see the problem with the breakpoint too depending on where you set it.
If you set the breakpoint on line rc = l.Tag no problem.

Best regards.
 
Upvote 0

skipsy

Member
Licensed User
Longtime User
:BangHead::BangHead::BangHead::BangHead::BangHead::BangHead:

I could have spend hours, weeks and more trying to understand what's
going on !!!

Now it sounds logical. As the program is stopped I guess many things are
still running in the system....

Thank you very much Klaus
William.
:sign0142:
 
Upvote 0
Top