Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check box question 1

Status
Not open for further replies.

fourofclubs

Programmer
May 13, 2003
18
0
0
CA
G'day,
I'm looking to have a check box on my form that allows you to choose one of three values, (0-unchecked, 1-checked, or 2-grayed). I can set it to grayed initially, but once it is clicked on, it can't be grayed again. Any thoughts?

Thanks,
Brad.
 
FYI:
The pre-defined constant for "2" is vbGrayed. The others are vbChecked and vbUnChecked.

Chip H.
 
Hi, thanks for replying.
I am aware that I can set the value property to 2 at run-time, but there are certain problems that arise when I click on the check box. I'd like to be able (when the box is clicked on) to cycle through the three values.
From what I can tell, when I click on the box, the click event procedure I've written fires off and does exactly what I'd like it to do. Then VB fires its own procedure to handle the click event (ie. it changes a checked box to an unchecked one, unchecked box to a checked one, or a grayed box to an unchecked one). So, when I click on an unchecked box, my procedure changes it to a checked box, and then VB comes along and changes it back into an unchecked one. Or, if I click on a checked box, my procedure changes it to a grayed box, and then VB changes that into an unchecked box. I think this is what's happening anyway.
Hopefully I've explained that somewhat clearly.

Thanks,
Brad.
 
Changing the value of a checkbox in it's click event will re-trigger the click event.

You can cycle through the 3 values from any other event with:
Check1.Value = (Check1.Value + 1) Mod 3


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
When you change the check box state with-in the click event, the click event will fire again.
This is probably what is causing the problem.
So, you need to prevent your own code fire executing a second time, once it has changed the state:

Private Sub Check1_Click()
Static bGetOut As Boolean
If bGetOut Then Exit Sub
bGetOut = True
'Your code to change the check box state here
bGetOut = False
End Sub
 
Thanks for your help, I've managed to get it working (though I'm sure there has to be a better way).
I tried using
Code:
Private Sub chkClosed_Click()
  Static bGetOut As Boolean
  If bGetOut Then Exit Sub
  bGetOut = True
  chkClosed = (chkClosed + 1) Mod 3
  bGetOut = False
End Sub

The problem with that was that the value of chkClosed was changed before entering the Click procedure (so if it was grayed or checked, it entered the procedure with an unchecked value, and there was no way of knowing which value it was). I had to stick the checked value into a static variable and it seems to work. Here's my solution:

Code:
Private Sub chkClosed_Click()
    Static bInitialize As Boolean
    Static intCheckValue As Integer
    Static bGetOut As Boolean
    If Not bInitialize Then
        intCheckValue = 2
        bInitialize = True
    End If
    If bGetOut Then Exit Sub
    bGetOut = True
    chkClosed = (intCheckValue + 1) Mod 3
    intCheckValue = chkClosed
    bGetOut = False
End Sub

Again, thanks a lot for your help,
Brad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top