Results 1 to 7 of 7

Thread: [RESOLVED] Save and restore CheckBox array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Resolved [RESOLVED] Save and restore CheckBox array

    I have an array of CheckBoxes from CheckBox(1) to CheckBox(32).
    Now I want to store the values (vbChecked/vbUnChecked) of the whole array in a sole number.
    And use that number to restore the values.

    What is an elegant method to achieve this?

  2. #2
    Lively Member
    Join Date
    Sep 2016
    Posts
    94

    Re: Save and restore CheckBox array

    clsIniFile by Dragokas is the right solution.

    https://www.vbforums.com/showthread....ght=clsIniFile

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Save and restore CheckBox array

    Store the 32 bits in a long.
    https://www.vbforums.com/showthread....es-into-a-Long

    Sample based on piece of code by Merri
    Code:
    Option Explicit
    
    Private Sub Form_Click()
      Dim lValue As Long
      Dim i As Long
      
      InitBitMask
      
      SetBit lValue, 3
      SetBit lValue, 5
      SetBit lValue, 17
      SetBit lValue, 23
      SetBit lValue, 25
      SetBit lValue, 32
      
      For i = 1 To 32
        Debug.Print "Bit " & CStr(i) & ": " & IsBitSet(lValue, i)
      Next i
      
    End Sub
    In a module:
    Code:
    Option Explicit
    
    Public BitMask(1 To 32) As Long
    
    Public Sub InitBitMask()
        BitMask(1) = &H1&
        BitMask(2) = &H2&
        BitMask(3) = &H4&
        BitMask(4) = &H8&
        BitMask(5) = &H10&
        BitMask(6) = &H20&
        BitMask(7) = &H40&
        BitMask(8) = &H80&
        BitMask(9) = &H100&
        BitMask(10) = &H200&
        BitMask(11) = &H400&
        BitMask(12) = &H800&
        BitMask(13) = &H1000&
        BitMask(14) = &H2000&
        BitMask(15) = &H4000&
        BitMask(16) = &H8000&
        BitMask(17) = &H10000
        BitMask(18) = &H20000
        BitMask(19) = &H40000
        BitMask(20) = &H80000
        BitMask(21) = &H100000
        BitMask(22) = &H200000
        BitMask(23) = &H400000
        BitMask(24) = &H800000
        BitMask(25) = &H1000000
        BitMask(26) = &H2000000
        BitMask(27) = &H4000000
        BitMask(28) = &H8000000
        BitMask(29) = &H10000000
        BitMask(30) = &H20000000
        BitMask(31) = &H40000000
        BitMask(32) = &H80000000
    End Sub
    
    Public Function IsBitSet(Value As Long, ByVal BitIndex As Integer) As Boolean
      IsBitSet = Value And BitMask(BitIndex)
    End Function
    
    Public Sub SetBit(ByRef Value As Long, ByVal BitIndex As Integer)
      Value = Value Xor BitMask(BitIndex)
    End Sub
    Last edited by Arnoutdv; Dec 1st, 2022 at 10:06 AM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: Save and restore CheckBox array

    Quote Originally Posted by Arnoutdv View Post
    Store the 32 bits in a long.
    That's exactly what I was after.
    Thank you!

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] Save and restore CheckBox array

    I would rename the function "SetBit" to "FlipBit" instead
    (because that's what it does currently - with the XOR-Op).

    Also (unless these functions are not used in a performance-critical, tight loop) -
    the preinit of a lookup-array seems a bit like overkill.

    Here's (all prefixed with "Bit_", to sit in the same "Intellisense-Region") a set of routines,
    which do not depend on any LookupArray-inits:
    Code:
    Public Function Bit_IsSet(Value As Long, ByVal BitIndex As Integer) As Boolean
      Bit_IsSet = Value And IIf(BitIndex = 32, &H80000000, 2 ^ (BitIndex - 1))
    End Function
    
    Public Sub Bit_Flip(Value As Long, ByVal BitIndex As Integer)
      Value = Value Xor IIf(BitIndex = 32, &H80000000, 2 ^ (BitIndex - 1))
    End Sub
    
    Public Sub Bit_Set(Value As Long, ByVal BitIndex As Integer)
      Value = Value Or IIf(BitIndex = 32, &H80000000, 2 ^ (BitIndex - 1))
    End Sub
    
    Public Sub Bit_Reset(Value As Long, ByVal BitIndex As Integer)
      Value = Value And Not IIf(BitIndex = 32, &H80000000, 2 ^ (BitIndex - 1))
    End Sub
    Olaf

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: [RESOLVED] Save and restore CheckBox array

    Quote Originally Posted by Schmidt View Post
    I would rename the function "SetBit" to "FlipBit" instead
    (because that's what it does currently - with the XOR-Op).
    I have to admit that I really don’t have a clue why I used Xor instead of Or. I’m aware of the difference..

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: [RESOLVED] Save and restore CheckBox array

    Quote Originally Posted by Schmidt View Post
    I would rename the function "SetBit" to "FlipBit" instead
    (because that's what it does currently - with the XOR-Op).
    Also (unless these functions are not used in a performance-critical, tight loop) -
    the preinit of a lookup-array seems a bit like overkill.
    Olaf
    Even better, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width