Android Question Checkboxes

Discussion in 'Android Questions' started by Arturs, Sep 18, 2015.

  1. Arturs

    Arturs Member Licensed User

    Hi

    I created eight checkboxes.

    Code:
    Dim b as Byte

    Sub CheckBox1_CheckedChange(Checked As Boolean)
        
    If CheckBox1.Checked = True Then Bit.And(b, 1)
    else Bit.Or(b, 1)

    endif

    End Sub

    Sub CheckBox2_CheckedChange(Checked As Boolean)

    If CheckBox2.Checked = True Then Bit.And(b,2)
    else Bit.Or(b, 2)
    endif

    End Sub
    Sub CheckBox3_CheckedChange(Checked As Boolean)
    ...
    End Sub
    Sub CheckBox4_CheckedChange(Checked As Boolean)
    ...
    End Sub
    Sub CheckBox5_CheckedChange(Checked As Boolean)
    ...
    End Sub
    Sub CheckBox6_CheckedChange(Checked As Boolean)
    ...
    End Sub
    Sub CheckBox7_CheckedChange(Checked As Boolean)
    ...
    End Sub
    ...
    Sub CheckBox8_CheckedChange(Checked As Boolean)
    I would not like to write very similar code in each checkboxes

    I would like to use something like this:

    Code:
    Sub Check_Checkboxes (number As Int)
       
    If CheckBox[number].Checked = True Then Bit.And(b, number)
    else Bit.Or(b, number)

    End Sub
    and use the following code for each checkboxes

    Code:
    Sub CheckBox1_CheckedChange(Checked As Boolean)
        
    Check_Checkboxes(
    1)

    endif


    Regards
    Artur
     
  2. DonManfred

    DonManfred Expert Licensed User

    Get some inspiration by this code

    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

        
    Private chklist As List
        
    Private chkarr(8As CheckBox

        
    Private CheckBox1 As CheckBox
        
    Private CheckBox2 As CheckBox
        
    Private CheckBox3 As CheckBox
        
    Private CheckBox4 As CheckBox
        
    Private CheckBox5 As CheckBox
        
    Private CheckBox6 As CheckBox
        
    Private CheckBox7 As CheckBox
        
    Private CheckBox8 As CheckBox
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        Activity.LoadLayout("EightCheckboxes")
        
    ' Fill Checkboxarray (Solution 1)
        chkarr(0) = CheckBox1
        chkarr(
    1) = CheckBox2
        chkarr(
    2) = CheckBox3
        chkarr(
    3) = CheckBox4
        chkarr(
    4) = CheckBox5
        chkarr(
    5) = CheckBox6
        chkarr(
    6) = CheckBox7
        chkarr(
    7) = CheckBox8
        
    '
        ' Trying Solution 1
        '
        ' Set all Textcolor to Blue
        For i = 0 To chkarr.Length-1
            chkarr(i).TextColor = 
    Colors.Blue
        
    Next
      
      
      
      
        
    ' Fill Checkboxlist (Solution 2)
        chklist.Initialize
        chklist.Add(CheckBox1)
        chklist.Add(CheckBox2)
        chklist.Add(CheckBox3)
        chklist.Add(CheckBox4)
        chklist.Add(CheckBox5)
        chklist.Add(CheckBox6)
        chklist.Add(CheckBox7)
        chklist.Add(CheckBox8)
        
    '
        ' Trying Solution 2
        '
        ' The List contains OBJECTs. To use it as a Checkbox
        ' you first need to cast the Object to an checkbox
        '
        ' Here we´ll set all Checkboxes to checked
        For i = 0 To chklist.Size-1
            
    Dim chk As CheckBox = chklist.Get(i)
            chk.Checked = 
    True
        
    Next
      
    End Sub
    PS: Welcome in our comunity!
     
  3. James Chamblin

    James Chamblin Active Member Licensed User

    Set all your checkboxes to the same event name. set each checkbox tag to its index. Then you can use Sender to find out which checkbox was checked.
    Code:
    Sub CheckBox_CheckedChange(Checked As Boolean)
        
    Dim CHeck As CheckBox = Sender
        
    Log (CHeck.Tag & " Was changed to " & Checked)
    End Sub
     
    mw71 and DonManfred like this.
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Arturs

    Arturs Member Licensed User

    Thank you for the so fast and precise answer
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice