Home All Groups Group Topic Archive Search About

Checkbox - how to clear without triggering click event

Author
23 Jun 2009 5:25 PM
Sheldon
Hello -

I have a form with a checkbox.  I am trying to clear the form and included
the code:

If ckComplaint.Value = 1 Then
   ckComplaint.Value = 0
End If

along with other code that clears textboxes, etc.  The above statement is
triggering the Click event of the checkbox instead of just changing the box
to blank.  How do I get around that?
--
Sheldon

Author
23 Jun 2009 6:52 PM
Bob Butler
Show quote Hide quote
"Sheldon" <Shel***@discussions.microsoft.com> wrote in message
news:015EF782-9369-4F66-B691-E43E624B2B79@microsoft.com...
> Hello -
>
> I have a form with a checkbox.  I am trying to clear the form and included
> the code:
>
> If ckComplaint.Value = 1 Then
>   ckComplaint.Value = 0
> End If
>
> along with other code that clears textboxes, etc.  The above statement is
> triggering the Click event of the checkbox instead of just changing the
> box
> to blank.  How do I get around that?

You set a flag and check in the click event

private mbFromCode as boolean

mbFromCode=True
ckComplaint.Value = vbUnchecked ' use constant for readability
mbFromCode = False

Private Sub ckComplaint_Click()
If Not mbFromCode Then
  ' user clicked
end if
end sub
Are all your drivers up to date? click for free checkup

Author
23 Jun 2009 9:02 PM
Richard Mueller [MVP]
Show quote Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:Ovm%23gPD9JHA.4976@TK2MSFTNGP04.phx.gbl...
>
> "Sheldon" <Shel***@discussions.microsoft.com> wrote in message
> news:015EF782-9369-4F66-B691-E43E624B2B79@microsoft.com...
>> Hello -
>>
>> I have a form with a checkbox.  I am trying to clear the form and
>> included
>> the code:
>>
>> If ckComplaint.Value = 1 Then
>>   ckComplaint.Value = 0
>> End If
>>
>> along with other code that clears textboxes, etc.  The above statement is
>> triggering the Click event of the checkbox instead of just changing the
>> box
>> to blank.  How do I get around that?
>
> You set a flag and check in the click event
>
> private mbFromCode as boolean
>
> mbFromCode=True
> ckComplaint.Value = vbUnchecked ' use constant for readability
> mbFromCode = False
>
> Private Sub ckComplaint_Click()
> If Not mbFromCode Then
>  ' user clicked
> end if
> end sub
>

The Click event fires if Form_Load code assigns a value to check the box
(like 1). It does not fire if you uncheck the box (with a 0), I assume
because this does not change the state of the check box. I use a boolean
flag whenever I only want a Click event Sub to run when the user initiates
the event, and not when my code does. However, I wasn't aware of the
vbUnchecked and vbChecked constants, which I like. Interesting that
Intellisense doesn't suggest them.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Author
23 Jun 2009 10:40 PM
Bob Butler
"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:%23i4PuaE9JHA.3544@TK2MSFTNGP04.phx.gbl...
<cut>
> The Click event fires if Form_Load code assigns a value to check the box
> (like 1). It does not fire if you uncheck the box (with a 0), I assume
> because this does not change the state of the check box.

Correct; if you check the box in design mode and then run the results would
be reversed.  Click fires when the state changes.

<cut>

> However, I wasn't aware of the vbUnchecked and vbChecked constants, which
> I like. Interesting that Intellisense doesn't suggest them.

If you look at the object browser the Value property is defined "As Integer"
so intellisense can't say anything about it.  If it had been declared "As
CheckBoxConstants" then it would show the options: vbChecked, vbUnchecked,
vbGrayed
Author
24 Jun 2009 12:17 AM
MikeD
"Bob Butler" <noway@nospam.ever> wrote in message
news:%234Xq8OF9JHA.3752@TK2MSFTNGP05.phx.gbl...
>
>> However, I wasn't aware of the vbUnchecked and vbChecked constants, which
>> I like. Interesting that Intellisense doesn't suggest them.
>
> If you look at the object browser the Value property is defined "As
> Integer" so intellisense can't say anything about it.  If it had been
> declared "As CheckBoxConstants" then it would show the options: vbChecked,
> vbUnchecked, vbGrayed


I've always wondered why MS didn't make the CheckBox's Value property As
CheckBoxConstants in order to provide Intellisense since there is that Enum
type. Only 2 things I've been able to come up with (not that I've ever put a
LOT of thought into this):

1.  Compatibility....Enums are Long and they needed to keep the Value
property an Integer. But I don't really know what compatibility issues that
could have caused.
2.  They simply overlooked it. But, it still could have been "fixed"
anywhere from VB4 to VB6 and any service pack in between.

Early on it bugged the bejeebers out of me because I could never remember
the constants for some reason (dunno why as they're pretty
straight-forward).

--
Mike
Author
25 Jun 2009 2:35 PM
Saga
What confuses some is that the option button uses a boolean type
to determine whether it is selected or not while the checkboxes
use a 0 or 1, not technically boolean. Saga


Show quoteHide quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:OHu7lEG9JHA.2120@TK2MSFTNGP02.phx.gbl...
>
> "Bob Butler" <noway@nospam.ever> wrote in message
> news:%234Xq8OF9JHA.3752@TK2MSFTNGP05.phx.gbl...
>>
>>> However, I wasn't aware of the vbUnchecked and vbChecked constants,
>>> which I like. Interesting that Intellisense doesn't suggest them.
>>
>> If you look at the object browser the Value property is defined "As
>> Integer" so intellisense can't say anything about it.  If it had been
>> declared "As CheckBoxConstants" then it would show the options:
>> vbChecked, vbUnchecked, vbGrayed
>
>
> I've always wondered why MS didn't make the CheckBox's Value property As
> CheckBoxConstants in order to provide Intellisense since there is that
> Enum type. Only 2 things I've been able to come up with (not that I've
> ever put a LOT of thought into this):
>
> 1.  Compatibility....Enums are Long and they needed to keep the Value
> property an Integer. But I don't really know what compatibility issues
> that could have caused.
> 2.  They simply overlooked it. But, it still could have been "fixed"
> anywhere from VB4 to VB6 and any service pack in between.
>
> Early on it bugged the bejeebers out of me because I could never remember
> the constants for some reason (dunno why as they're pretty
> straight-forward).
>
> --
> Mike
>
>
Author
25 Jun 2009 2:55 PM
MikeD
"Saga" <antiSpam@nowhere.com> wrote in message news:%23KYNDJa9JHA.200@TK2MSFTNGP05.phx.gbl...
> What confuses some is that the option button uses a boolean type
> to determine whether it is selected or not while the checkboxes
> use a 0 or 1, not technically boolean. Saga
>

That's because a checkbox is a tri-state...checked, unchecked, or grayed....another reason to use the constants instead of the
literals 0, 1, or 2.

--
Mike

Bookmark and Share