PC Review


Reply
Thread Tools Rate Thread

How to Check and Uncheck CheckBox without Raising an Events

 
 
=?Utf-8?B?V29vIE11biBGb29uZw==?=
Guest
Posts: n/a
 
      27th Oct 2004
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?
 
Reply With Quote
 
 
 
 
Larry Serflaten
Guest
Posts: n/a
 
      27th Oct 2004

"Woo Mun Foong" <(E-Mail Removed)> wrote

> 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

 
Reply With Quote
 
=?Utf-8?B?QWxiZXJ0?=
Guest
Posts: n/a
 
      27th Oct 2004
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



"Woo Mun Foong" wrote:

> 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?

 
Reply With Quote
 
Charles Law
Guest
Posts: n/a
 
      27th Oct 2004
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


"Woo Mun Foong" <(E-Mail Removed)> wrote in message
news:93F06CAF-2FA5-467F-A1F2-(E-Mail Removed)...
>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?



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
uncheck and check checkbox. =?Utf-8?B?RS1tYWlsIHJlcG9ydCB1c2luZyBMb3R1cyBOb3Rl Microsoft Access 3 19th May 2007 01:01 AM
check/uncheck a checkbox leonidas Microsoft Excel Programming 3 6th Jul 2006 11:24 AM
Check and uncheck a checkbox NoProgram Microsoft Excel Programming 3 25th Apr 2006 10:44 PM
how to check/uncheck the checkbox in datagrid =?Utf-8?B?UmVraGE=?= Microsoft Dot NET Framework Forms 4 2nd Dec 2004 04:54 PM
how to check/uncheck the checkbox in datagrid =?Utf-8?B?UmVraGE=?= Microsoft ASP .NET 2 2nd Dec 2004 03:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:42 PM.