Checkbox event handler

jimseng

Member
Licensed User
Longtime User
Hi.
I want to find out which checkbox has had is checked state changed. This is from the example here: http://www.b4x.com/forum/basic4andr...7221-list-two-columns-checkbox.html#post41354

The example shows how to go through the whole list and find out which checkboxes are checked. I want to find out which checkbox checked state has changed as soon as it is clicked:


Sub Process_Globals
End Sub
Sub Globals
Dim ScrollView1 As ScrollView
Dim lstChecks As List
Dim height As Int
height = 50dip
Dim chk As CheckBox
End Sub
Sub Activity_Create(FirstTime As Boolean)
ScrollView1.Initialize(0)
Dim pnl As Panel
pnl = ScrollView1.Panel
Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
lstChecks.Initialize
For i = 1 To 10
chk.Initialize("chkchng")
chk.Text = "Item #" & i
chk.tag = i
lstChecks.Add(chk)
Dim lbl1 As Label
pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
Next
pnl.Height = lstChecks.Size * height
End Sub

Sub Chkchng_CheckedChange(Checked As Boolean)
'show which checkbox has been clicked
End Sub
 

jimseng

Member
Licensed User
Longtime User
Sorry. I am new to this. The answer is easy.

Dim send As CheckBox
Send=Sender
ToastMessageShow(send.text & " " & send.Checked,False)
 
Upvote 0

dbcrosby

Member
Licensed User
Longtime User
Message Not showing thr corect check box.

I've tried your example but it only shows the last Item #10 true even thou I checked check box #5. Can you help me?

Thanks

Sub Process_Globals
End Sub
Sub Globals
Dim ScrollView1 As ScrollView
Dim lstChecks As List
Dim height As Int
height = 50dip
Dim chk As CheckBox
Dim pnl As Panel

End Sub
Sub Activity_Create(FirstTime As Boolean)
ScrollView1.Initialize(0)
' Dim pnl As Panel
pnl = ScrollView1.Panel
Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
lstChecks.Initialize
For i = 1 To 10
chk.Initialize("chkchng")
chk.Text = "Item #" & i
chk.tag = i
lstChecks.Add(chk)
Dim lbl1 As Label
pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
Next
pnl.Height = lstChecks.Size * height
End Sub

Sub Chkchng_CheckedChange(Checked As Boolean)
'show which checkbox has been clicked

Dim send As CheckBox
Send=Sender
ToastMessageShow(send.text & " " & send.Checked,False)

End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is because you are using the same checklist object 10 times and adding the same pointer to the list. You need to Dim a new checklist for each one:

B4X:
For i = 1 To 10
  Dim chk As CheckBox
  chk.Initialize("chkchng")
  chk.Text = "Item #" & i
  chk.tag = i
  lstChecks.Add(chk)
  .
  .
  .

This will give you a new object each time. You need to remove chk from the global variables and Dim it as you need to access the object.

Dim chk As checkBox
chk=lstChecks.get(n)
 
Last edited:
Upvote 0

dbcrosby

Member
Licensed User
Longtime User
Rotate looses checked items

I have a new problem that when I rotate the device it looses the items that I had checked.
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
I think Steve meant for you to add:
Dim chk As CheckBox
Thank you Mahares, this code works now.
B4X:
Sub Process_Globals

End Sub

Sub Globals

    Dim ScrollView1 As ScrollView
    Dim lstChecks As List
    Dim height As Int
    height = 50dip
    'Dim chk As CheckBox
    Dim pnl As Panel

End Sub

Sub Activity_Create(FirstTime As Boolean)

    ScrollView1.Initialize(0)
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
    lstChecks.Initialize

    For i = 1 To 10
        Dim chk As CheckBox
        'chk=lstChecks.get(n) '-- original --
        chk.Initialize("chkchng")
        chk.Text = "Item #" & i
        chk.tag = i
        lstChecks.Add(chk)
'        Dim lbl1 As Label  '-- never used --
        pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
    Next

    pnl.Height = lstChecks.Size * height

End Sub

Sub Chkchng_CheckedChange(Checked As Boolean)
  
'-- show which checkbox has been clicked
    Dim send As CheckBox
    send = Sender
    ToastMessageShow(send.text & " " & send.Checked, False)
        Log(""& send.text &" "& send.Checked &" ")
End Sub
If I want to give another colour to the clicked Panel, I tried some coding, but I have different results.
For example I'll get every panel in the color red with this code:
B4X:
'-- Test with colors --
    For i = 1 To 10
        Dim PanelNummer As Int
        PanelNummer = i
            Log(""& PanelNummer &" "& send.Checked &" ")
        pnl.Tag = PanelNummer
      
'        If ""& send.text &" "& send.Checked &"" = "Item #"&(i)&" true" Then  '-- works only with Number 10 --
'        If ""& PanelNummer &"" = ""&(i)&"" Then  '-- gets all Panels in Color red --
        If PanelNummer = i Then  '-- gets all Panels in Color red --
            pnl.Color = Colors.Red
        Else
            pnl.Color = Colors.Black      
        End If
    Next

I'm just playing around with this code, it is not really important to me to know a solution, but I'm interested ... (if anyone knows something without having much work on it). :)
 
Last edited:
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Sorry Erel,

why is it better to start a new thread?

Isn't it good to hold an "old" thread alive, because there are all discussed points in one view?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I think Steve meant for you to add:
Dim chk As CheckBox
Yes, that's what it should have been thanks, changed the code to avoid future confusion.
 
Upvote 0
Top