How to Check and Uncheck CheckBox without Raising an Events

G

Guest

I have a checkbox, when enable, allows me to proceed with what I like to do.
However, I need to check a certain conditions before I allow the checked box
to be checked, if condition is not fullfill I need to cancel the Checked
State.
I captured the Mouse Click event inside CheckedChanged and
CheckedStateChanged Events but it goes into infinite loops.

I am using VB.NET 2003 and .NET 1.1

Briefly, my code is as follows:-

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If (Condition1=False) then
If (CheckBox1.Checked)Then
MsgBox ("Condition 1 False cannot proceed!")
CheckBox1.Checked = False
End If
Else
'Do my thing
End If
End Sub


The code doesn't let me unchecked the checkbox. Is there any Cancel method
for Checkbox Events?
 
L

Larry Serflaten

Woo Mun Foong said:
The code doesn't let me unchecked the checkbox. Is there any Cancel method
for Checkbox Events?

No, you can't cancel the events other than to disable the control.

What you can do is avoid recursion like:

If Not CheckBox1.Tag Is Nothing Then Exit Sub
CheckBox1.Tag = CheckBox1
'.... do your condition test here
CheckBox1.Tag = Nothing

That stuffs a reference into the Tag property such that any later changes
will cause the event to exit early. When you are done, reset the Tag to Nothing
to allow the next (user) change to enter into the event....

LFS
 
G

Guest

hi Mun Fong,

just need slight change to your code ..check the checkstate so that it
bypass the checked = false

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
if checkbox1.checkState = checkstate.checked then
If (Condition1=False) then
If (CheckBox1.Checked)Then
MsgBox ("Condition 1 False cannot proceed!")
CheckBox1.Checked = False
End If
Else
'Do my thing
End If
end if
End Sub


Albert
 
C

Charles Law

Hi

Checkboxes have an AutoCheck property. Set this to False at design time and
no event will be raised when you set the Checked property in code. You then
need to handle the Click event and toggle the checked state, e.g.

<code>
Private Sub Check1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Check1.Click

Check1.Checked = Not Check1.Checked

End Sub
</code>

This causes the checked state to change when the user clicks your checkbox.

HTH

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top