Announcement

Collapse
No announcement yet.

Flipping the checked state of a menu item

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Flipping the checked state of a menu item

    I wrote these two lines of code, which flip the check state of a menu item between %MF_CHECKED (&H8) and %MF_UNCHECKED (&H0).

    It works, but only because one of the two values was a zero, making it possible to use the IsFalse the way I did.

    Code:
    Menu Get State hMenuOptions, ByCmd %ID_Backup To iReturn
    Menu Set State hMenuOptions, ByCmd %ID_Backup, Abs(IsFalse iReturn)*8
    Is there a more common approach used in PowerBASIC land? In my previous world, the two values were 0/1 and accessed directly as a property, making the flip an easy one line of code.

    If both values were non-zero, this longer code would also work:

    Code:
    IF iState = 1stvalue Then
       iState = 2ndValue
    Else
       iState = 1stValue
    End if
    But elegant it's not.

    Just thought I'd see if there was a common, simpler approach that I've not run across.

  • #2
    Generally bit masks are managed with the boolean operators AND, OR, NOT, etc.

    Untested but should work..
    Code:
       Menu get State  ....   to iState    
       Menu set State  .....  IIF&(iState AND %MF_CHECKED), %MF_UNCHECKED, %MF_CHECKED)
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thanks, Micheal.

      Your approach would work regardless of the values of the two options. Neither has to be zero.

      Comment


      • #4
        Bit Values can be toggled by using XOR.

        Where:
        Code:
           Local Flags As Dword
           
           Flags = 1  ' Bit 0 ON
           
           Flags = (Flags Xor 2)  ' Toggle Bit 1 (Value of 2) (was off will be on)
           
           ? Format$(Flags)       ' verify that both Bits 0 and 1 are on  (Values 1+2 should = 3)
           
           Flags = (Flags Xor 2)  ' Toggle Bit 1 (Value of 2) (was on will be off)
           
           ? Format$(Flags)       ' verify that Bit 1 remains on, but Bit 2 is back off (should = 1)
        Turn them on with OR

        Turn them off with AND NOT
        Scott Slater
        Summit Computer Networks, Inc.
        www.summitcn.com

        Comment


        • #5
          Hi Scott,

          Bit flipping as you describe works well for a case like %MF_CHECKED (&H8) and %MF_UNCHECKED (&H0), where a flipping a single bit would convert between the desired values. I like that.

          In a more general case, flipping between two values could require multiple bit flips, unique to the numbers involved.

          My post was to inquire if there are some general algorithms, like these:

          Toggle between 1 and 0
          Code:
          A = NOT A           (well, in VB anyway)
          Toggle between -1 and 0
          Code:
          x = x XOR 1
          Toggle between 1 and 2
          Code:
          x = 3-x
          And, as I noted, it's always easy to use

          Code:
          IF iState = 1stvalue Then
             iState = 2ndValue
          Else
             iState = 1stValue
          End if
          For the special cases where a single bit distinguishes two values, the use of XOR as you've noted would work well.
          Last edited by Gary Beene; 6 Mar 2009, 11:03 AM.

          Comment


          • #6
            Originally posted by Gary Beene View Post
            In a more general case, flipping between two values could require multiple bit flips, unique to the numbers involved.
            If you mean flipping 2 bits at once, first OR them together then flip them both using XOR.

            Code:
            Bits2Flip = %Property1 OR %Property2
            
            BitField = (BitField XOR Bits2Flip)
            I am not sure if this is what you are looking for or not.
            Scott Slater
            Summit Computer Networks, Inc.
            www.summitcn.com

            Comment


            • #7
              Gary, if I understand your request, maybe you could use

              x = IFF(x = u1, u2, u1)

              to toggle between x = u1 and x = u2

              Comment

              Working...
              X