Android Question How to check multiple checkbox at a time without adding tag

Nitin Joshi

Active Member
Licensed User
Longtime User
In my program there are 6 check boxes. I would like to execute some task only with condition at-least one check box is selected. At present i have used below code. Is there any better way?
Code: Copy
If Not(chk1.Checked Or chk2.Checked Or chk3.Checked Or chk4.Checked Or chk5.Checked Or chk6.Checked) Then
    'some task
End If
 
If you can use a common _CheckedChange sub then possibly
edit: or you could put the +1 / -1 bit in each _CheckedChange sub
Code: Copy
Sub CommonCheckbox_CheckedChange(Checked As Boolean)
       if checked then 
             BoxChecked = boxchecked + 1
      else
             BoxChecked = boxchecked - 1
      end if
       if boxchecked > 6 then boxchecked = 6 'shouldn't be needed but........
       if boxchecked < 0 then boxhecked = 0   'shouldn't be needed but........
end sub
if boxchecked > 0 then some task 'if at least one is checked
if boxchecked = 6 then some other task 'if all are checked
 
Last edited:
Upvote 0
B4X: Copy
Sub CheckBoxTrue(pnl As Panel) As Boolean
    For Each v As View In pnl.GetAllViewsRecursive
        If v Is CheckBox Then
            Dim chk As CheckBox = v
            If chk.Checked Then Return True
        End If
    Next
    
    Return False
End Sub
 
Upvote 0
If you can use a common _CheckedChange sub then possibly
edit: or you could put the +1 / -1 bit in each _CheckedChange sub
Code: Copy
Sub CommonCheckbox_CheckedChange(Checked As Boolean)
       if checked then
             BoxChecked = boxchecked + 1
      else
             BoxChecked = boxchecked - 1
      end if
       if boxchecked > 6 then boxchecked = 6 'shouldn't be needed but........
       if boxchecked < 0 then boxhecked = 0   'shouldn't be needed but........
end sub
if boxchecked > 0 then some task 'if at least one is checked
if boxchecked = 6 then some other task 'if all are checked
Dear RJB, Thanks but I am sorry as I am not clear. I have not understood your idea.
 
Upvote 0
B4X: Copy
Sub CheckBoxTrue(pnl As Panel) As Boolean
    For Each v As View In pnl.GetAllViewsRecursive
        If v Is CheckBox Then
            Dim chk As CheckBox = v
            If chk.Checked Then Return True
        End If
    Next
   
    Return False
End Sub
Thanks, like your idea to use panel to group check boxes.....
 
Upvote 0
Yes, that's right one is added each time a box is checked, one is taken away each time a box is unchecked. If the total is zero then no boxes are checked if greater than zero then at least one is checked and if six then all are checked.
 
Upvote 0
If you always want to have only one CheckBox selected use RadioButtons instead they are designed exactly for that.
You can group RadioButtons on Panels. RadioButtons on different Panels don't interact.
Dear Klaus, I want one or more CheckBox selection...
 
Upvote 0
I only did tests with with checkbox.
 

Attachments

  • Screenshot_20201219-223736.png
    Screenshot_20201219-223736.png
    25.9 KB · Views: 583
  • checktest.zip
    checktest.zip
    9.7 KB · Views: 413
Upvote 0
Back
Top