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

Nitin Joshi

Active Member
Licensed 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?
Check Box Value Return:
If Not(chk1.Checked Or chk2.Checked Or chk3.Checked Or chk4.Checked Or chk5.Checked Or chk6.Checked) Then
    'some task
End If
 

RJB

Active Member
Licensed User
Longtime User
If you can use a common _CheckedChange sub then possibly
edit: or you could put the +1 / -1 bit in each _CheckedChange sub
B4X:
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

angel_

Well-Known Member
Licensed User
Longtime User
B4X:
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

Nitin Joshi

Active Member
Licensed User
If you can use a common _CheckedChange sub then possibly
edit: or you could put the +1 / -1 bit in each _CheckedChange sub
B4X:
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

Nitin Joshi

Active Member
Licensed User
B4X:
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

AnandGupta

Expert
Licensed User
Longtime User
Dear Nitin,

RJB logic is when a check box is clicked, the click event adds 1 in a variable, so check this variable value.

Regards

Anand
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
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

Nitin Joshi

Active Member
Licensed User
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

TILogistic

Expert
Licensed User
Longtime User
Thanks, like your idea to use panel to group check boxes.....
Activity:

Update:
B4X:
Sub VerifyCheckBox
    For Each v As B4XView In Activity.GetAllViewsRecursive
        If v Is CheckBox Then
            If  v.Checked Then  v.Text = "True" Else v.Text = "False"
        End If
    Next
End Sub
 

Attachments

  • check.zip
    9.3 KB · Views: 130
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I only did tests with with checkbox.
 

Attachments

  • Screenshot_20201219-223736.png
    Screenshot_20201219-223736.png
    25.9 KB · Views: 172
  • checktest.zip
    9.7 KB · Views: 137
Upvote 0
Top