Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tri-state problem (Word97/SR2)

    The code below doesn't work, really. Today I'll mull over a better solution. In the meantime .....

    The problem probably has a name, such as "Tri-State problem", extendable to four or more states.

    Here's the scenario: A user GUI with three options - Upper case, Lower case and Title case makes a good example. The obvious combinations that can be applied to selected text are
    1) Upper case only
    2) Lower case only
    3) Title case only
    4) Not one of Upper, Lower or Title case


    We want to do as much as possible for the user, so the impulse is to code each check box so that when the state is OFF and the user clicks the check box, we turn the state ON and turn OFF all other (two) associated checkboxes.

    Unfortunately, turning "OFF" the other checkboxes seems to correspond to a Click event on THAT checkbox (or a MouseUp or a Change etc), and so the user never quite regains control of the GUI form.

    Radio buttons have been considered and dropped, because ONE button is always left turned ON, which obstructs our combination (4) above.


    I'm obviously nowhere near as good at logic or VBA as I think I am.

    I throw this problem out for suggestions.....

    <pre>Private Sub cbLower_Click()
    If Me.cbLower Then
    Me.cbLower = False
    Else
    Me.cbLower = True
    Me.cbUpper = False
    Me.cbTitle = False
    End If
    End Sub

    Private Sub cbTitle_Click()
    If Me.cbTitle Then
    Me.cbTitle = False
    Else
    Me.cbTitle = True
    Me.cbLower = False
    Me.cbUpper = False
    End If
    End Sub

    Private Sub cbUpper_Click()
    If Me.cbUpper Then
    Me.cbUpper = False
    Else
    Me.cbUpper = True
    Me.cbLower = False
    Me.cbTitle = False
    End If
    End Sub
    </pre>


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Tri-state problem (Word97/SR2)

    Chris,
    Why not have combination 4 as a None of the Above option?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Tri-state problem (Word97/SR2)

    Chris,

    There is no need to set the value of a checkbox in its own OnClick event procedure, that is done for you automatically; you only need to turn the others off if necessary. Although the code will still call the OnClick event procedure for the other checkboxes if they are "On", this process will run out of steam once all other check boxes are off. Here is the revised code:<pre>Private Sub cbLower_Click()
    If Me.cbLower Then
    Me.cbUpper = False
    Me.cbTitle = False
    End If
    End Sub

    Private Sub cbTitle_Click()
    If Me.cbTitle Then
    Me.cbLower = False
    Me.cbUpper = False
    End If
    End Sub

    Private Sub cbUpper_Click()
    If Me.cbUpper Then
    Me.cbLower = False
    Me.cbTitle = False
    End If
    End Sub</pre>

    There are occasions when you want to prevent one event from triggering of another. You can handle that by using a module level or global boolean variable, say blnDontDoThis. In each event procedure where you need it, test blnDontDoThis. If the variable is True, exit the event procedure. Otherwise, set it to True, do your thing and set it to False again.

    In this example, it is not really necessary, but this is what it would look like:<pre>Private blnDontDoThis As Boolean

    Private Sub cbLower_Click()
    If blnDontDoThis Then Exit Sub
    blnDontDoThis = True
    If Me.cbLower Then
    Me.cbUpper = False
    Me.cbTitle = False
    End If
    blnDontDoThis = False
    End Sub

    Private Sub cbTitle_Click()
    If blnDontDoThis Then Exit Sub
    blnDontDoThis = True
    If Me.cbTitle Then
    Me.cbLower = False
    Me.cbUpper = False
    End If
    blnDontDoThis = False
    End Sub

    Private Sub cbUpper_Click()
    If blnDontDoThis Then Exit Sub
    blnDontDoThis = True
    If Me.cbUpper Then
    Me.cbLower = False
    Me.cbTitle = False
    End If
    blnDontDoThis = False
    End Sub</pre>

    You can see how it works by single-stepping through it.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tri-state problem (Word97/SR2)

    > Why not have combination 4 as a None of the Above option

    Rry, thanks for the reply. We had considered a fourth control to turn off all the others - either a command button or an fourth checkbox, but wanted to reduce the amount of clutter on the form. As Hans has tried not to point out, I just didn't have my brain in the correct gear!

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tri-state problem (Word97/SR2)

    > There is no need to set the value of a checkbox in its own OnClick event procedure

    Thanks, Hans. You have confirmed in me either my absymal ignorance of controls OR my inability to get my brain into gear on days that end with the letter "y". (grin)

    A few strategically-placed comment-quotes have done the trick, and the form works as it should - three checkbox buttons that do what the user wants with a minimum of fuss.

    I felt sure that there had to be a way to get the job done in a neat (engineering) fashion, but kept running into the obstacle brought about by my determination to set the checkbox.



    > using a module level or global boolean variable

    This too I had considered, but in general, now, I shy away from globals wherever possible. I felt sure that there was a clean method, but was unable to step back and see it.

    Thanks again.

    I'll mark you down for a bonus at Christmas time.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Tri-state problem (Word97/SR2)

    <hr>> using a module level or global boolean variable

    This too I had considered, but in general, now, I shy away from globals wherever possible. I felt sure that there was a clean method, but was unable to step back and see it.<hr>
    You could get the same effect without a global by using a static variable in a single procedure that you call from each of the button click events (pass the clicked button as an object). Something like...

    Private Sub cbLower_Click()
    If cbLower.Value then DoMyThing (cbLower)
    End Sub

    Private Sub DoMyThing(cbIn As CheckBox)
    Static blnAlreadyHere As Boolean

    If blnAlreadyHere Then Exit Sub
    blnAlreadyHere = True
    cbUpper.Value = False
    cbLower.Value = False
    cbTitle.Value = False
    cbIn.Value = True
    blnAlreadyHere = False
    End Sub

    StuartR

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tri-state problem (Word97/SR2)

    > a single procedure that you call from each of the button click events

    Stuart, thanks for this. I'll not use it in my original probblem but will file it for future reverence (sic!).

    There will come a time when I want to perform a global housekeeping for a set of controls, I'm sure.

Posting Permissions

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