PC Review


Reply
Thread Tools Rate Thread

how to cancel checkChanged or click event for a checkbox?

 
 
=?Utf-8?B?UmljaA==?=
Guest
Posts: n/a
 
      7th Aug 2007
Is there a cancel argument for cancelling if you want to check or uncheck a
checkbox?

In the checkChanged event of a checkbox I ask the user if they are sure they
want to check/uncheck something. If No, I want to cancel the check or
uncheck and cancel the click event. Right now I have a form level boolean
checkvar that I set based on the user's response. If No then when entering
the click event I don't perform various actions. Is there a way to prevent
the click event from happening if the user selects to Not continue with the
check change?

Thanks,
Rich
 
Reply With Quote
 
 
 
 
rowe_newsgroups
Guest
Posts: n/a
 
      7th Aug 2007
On Aug 7, 12:22 pm, Rich <R...@discussions.microsoft.com> wrote:
> Is there a cancel argument for cancelling if you want to check or uncheck a
> checkbox?
>
> In the checkChanged event of a checkbox I ask the user if they are sure they
> want to check/uncheck something. If No, I want to cancel the check or
> uncheck and cancel the click event. Right now I have a form level boolean
> checkvar that I set based on the user's response. If No then when entering
> the click event I don't perform various actions. Is there a way to prevent
> the click event from happening if the user selects to Not continue with the
> check change?
>
> Thanks,
> Rich


Off the top of my head I would do this:

Private Sub checkBox_CheckedChanged(ByVal sender As Object, ByVal
e As EventArgs)
If MessageBox.Show("Are you sure", "Please Confirm",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
Dim cb As CheckBox = DirectCast(sender, CheckBox)
RemoveHandler cb.CheckedChanged, AddressOf
checkBox_CheckedChanged
cb.Checked = Not cb.Checked
AddHandler cb.CheckedChanged, AddressOf
checkBox_CheckedChanged
End If
End Sub

That should revert the checkbox to it's previous state if the user
does not confirm their choice. The RemoveHandler/AddHandler is used to
prevent the changing of the checkbox's checked state from triggering
the event handler recursively.

Thanks,

Seth Rowe

 
Reply With Quote
 
=?Utf-8?B?UmljaA==?=
Guest
Posts: n/a
 
      7th Aug 2007
Thank you. I see your method will affect any checkbox on the form. The is
definitely more efficient than the other thing I tried.

I changed my form level boolean to a boolean within the click event of the
checkbox. upon clicking - the boolean takes on the value of the checkstate
-- this is to prevent the recursion thing as you noted. Then I ask the user
yes/no in the click event. If no, I set the checkstate to the opposite of
the boolean.

This appears to be working, and I do have several checkboxes, but this
particular one is the only one where the users asked me to implement this
checking mechanism.

With your mechanism, you only need to add the code once (and perhaps specify
which checkboxes to affect). My mechanism would require the same code for
all the checkboxes. . So even though, I only need this to happen to one
checkbox (for now), I think I will go with your mechansim.

Thanks again,
Rich

"rowe_newsgroups" wrote:

> On Aug 7, 12:22 pm, Rich <R...@discussions.microsoft.com> wrote:
> > Is there a cancel argument for cancelling if you want to check or uncheck a
> > checkbox?
> >
> > In the checkChanged event of a checkbox I ask the user if they are sure they
> > want to check/uncheck something. If No, I want to cancel the check or
> > uncheck and cancel the click event. Right now I have a form level boolean
> > checkvar that I set based on the user's response. If No then when entering
> > the click event I don't perform various actions. Is there a way to prevent
> > the click event from happening if the user selects to Not continue with the
> > check change?
> >
> > Thanks,
> > Rich

>
> Off the top of my head I would do this:
>
> Private Sub checkBox_CheckedChanged(ByVal sender As Object, ByVal
> e As EventArgs)
> If MessageBox.Show("Are you sure", "Please Confirm",
> MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
> Dim cb As CheckBox = DirectCast(sender, CheckBox)
> RemoveHandler cb.CheckedChanged, AddressOf
> checkBox_CheckedChanged
> cb.Checked = Not cb.Checked
> AddHandler cb.CheckedChanged, AddressOf
> checkBox_CheckedChanged
> End If
> End Sub
>
> That should revert the checkbox to it's previous state if the user
> does not confirm their choice. The RemoveHandler/AddHandler is used to
> prevent the changing of the checkbox's checked state from triggering
> the event handler recursively.
>
> Thanks,
>
> Seth Rowe
>
>

 
Reply With Quote
 
=?Utf-8?B?UmljaA==?=
Guest
Posts: n/a
 
      7th Aug 2007
The only problem I am having with the RemoveHandler method is that I also
have to remove the Handler for the click event in order to not go to it. But
then I need to re-add that handler. Where is the best place to re-add the
click handler?

"Rich" wrote:

> Thank you. I see your method will affect any checkbox on the form. The is
> definitely more efficient than the other thing I tried.
>
> I changed my form level boolean to a boolean within the click event of the
> checkbox. upon clicking - the boolean takes on the value of the checkstate
> -- this is to prevent the recursion thing as you noted. Then I ask the user
> yes/no in the click event. If no, I set the checkstate to the opposite of
> the boolean.
>
> This appears to be working, and I do have several checkboxes, but this
> particular one is the only one where the users asked me to implement this
> checking mechanism.
>
> With your mechanism, you only need to add the code once (and perhaps specify
> which checkboxes to affect). My mechanism would require the same code for
> all the checkboxes. . So even though, I only need this to happen to one
> checkbox (for now), I think I will go with your mechansim.
>
> Thanks again,
> Rich
>
> "rowe_newsgroups" wrote:
>
> > On Aug 7, 12:22 pm, Rich <R...@discussions.microsoft.com> wrote:
> > > Is there a cancel argument for cancelling if you want to check or uncheck a
> > > checkbox?
> > >
> > > In the checkChanged event of a checkbox I ask the user if they are sure they
> > > want to check/uncheck something. If No, I want to cancel the check or
> > > uncheck and cancel the click event. Right now I have a form level boolean
> > > checkvar that I set based on the user's response. If No then when entering
> > > the click event I don't perform various actions. Is there a way to prevent
> > > the click event from happening if the user selects to Not continue with the
> > > check change?
> > >
> > > Thanks,
> > > Rich

> >
> > Off the top of my head I would do this:
> >
> > Private Sub checkBox_CheckedChanged(ByVal sender As Object, ByVal
> > e As EventArgs)
> > If MessageBox.Show("Are you sure", "Please Confirm",
> > MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
> > Dim cb As CheckBox = DirectCast(sender, CheckBox)
> > RemoveHandler cb.CheckedChanged, AddressOf
> > checkBox_CheckedChanged
> > cb.Checked = Not cb.Checked
> > AddHandler cb.CheckedChanged, AddressOf
> > checkBox_CheckedChanged
> > End If
> > End Sub
> >
> > That should revert the checkbox to it's previous state if the user
> > does not confirm their choice. The RemoveHandler/AddHandler is used to
> > prevent the changing of the checkbox's checked state from triggering
> > the event handler recursively.
> >
> > Thanks,
> >
> > Seth Rowe
> >
> >

 
Reply With Quote
 
=?Utf-8?B?UmljaA==?=
Guest
Posts: n/a
 
      7th Aug 2007
OK. I modified the original Yes/No --RemoveHandler method to an

If yes/no then
....
removehandler...
Else
addHandler...click...
End If

Now I have to nice dynamic way to handle Checkboxes.

"Rich" wrote:

> The only problem I am having with the RemoveHandler method is that I also
> have to remove the Handler for the click event in order to not go to it. But
> then I need to re-add that handler. Where is the best place to re-add the
> click handler?
>
> "Rich" wrote:
>
> > Thank you. I see your method will affect any checkbox on the form. The is
> > definitely more efficient than the other thing I tried.
> >
> > I changed my form level boolean to a boolean within the click event of the
> > checkbox. upon clicking - the boolean takes on the value of the checkstate
> > -- this is to prevent the recursion thing as you noted. Then I ask the user
> > yes/no in the click event. If no, I set the checkstate to the opposite of
> > the boolean.
> >
> > This appears to be working, and I do have several checkboxes, but this
> > particular one is the only one where the users asked me to implement this
> > checking mechanism.
> >
> > With your mechanism, you only need to add the code once (and perhaps specify
> > which checkboxes to affect). My mechanism would require the same code for
> > all the checkboxes. . So even though, I only need this to happen to one
> > checkbox (for now), I think I will go with your mechansim.
> >
> > Thanks again,
> > Rich
> >
> > "rowe_newsgroups" wrote:
> >
> > > On Aug 7, 12:22 pm, Rich <R...@discussions.microsoft.com> wrote:
> > > > Is there a cancel argument for cancelling if you want to check or uncheck a
> > > > checkbox?
> > > >
> > > > In the checkChanged event of a checkbox I ask the user if they are sure they
> > > > want to check/uncheck something. If No, I want to cancel the check or
> > > > uncheck and cancel the click event. Right now I have a form level boolean
> > > > checkvar that I set based on the user's response. If No then when entering
> > > > the click event I don't perform various actions. Is there a way to prevent
> > > > the click event from happening if the user selects to Not continue with the
> > > > check change?
> > > >
> > > > Thanks,
> > > > Rich
> > >
> > > Off the top of my head I would do this:
> > >
> > > Private Sub checkBox_CheckedChanged(ByVal sender As Object, ByVal
> > > e As EventArgs)
> > > If MessageBox.Show("Are you sure", "Please Confirm",
> > > MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
> > > Dim cb As CheckBox = DirectCast(sender, CheckBox)
> > > RemoveHandler cb.CheckedChanged, AddressOf
> > > checkBox_CheckedChanged
> > > cb.Checked = Not cb.Checked
> > > AddHandler cb.CheckedChanged, AddressOf
> > > checkBox_CheckedChanged
> > > End If
> > > End Sub
> > >
> > > That should revert the checkbox to it's previous state if the user
> > > does not confirm their choice. The RemoveHandler/AddHandler is used to
> > > prevent the changing of the checkbox's checked state from triggering
> > > the event handler recursively.
> > >
> > > Thanks,
> > >
> > > Seth Rowe
> > >
> > >

 
Reply With Quote
 
New Member
Join Date: Jul 2010
Posts: 1
 
      14th Jul 2010
There is a much better way and a whole lot easier and will be more stable coding. Here is my solution that I use all the time and I hope it helps.

To test. Just copy and paste into your own code.

Public Class Form1
'Create a New Custom CheckBox class and override the Click event
Public Class clsMyCustomCheckBox
Inherits CheckBox
Public IsReadOnly As Boolean
Private PriorState As System.Windows.Forms.CheckState
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
PriorState = Me.CheckState
If IsReadOnly Then
Me.CheckState = PriorState
Else
MyBase.OnClick(e)
End If
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyTestCheckBox As clsMyCustomCheckBox = New clsMyCustomCheckBox
MyTestCheckBox.Text = "Go ahead and Try to change my Click State !!!"
MyTestCheckBox.AutoSize = True
Me.Controls.Add(MyTestCheckBox)
MyTestCheckBox.IsReadOnly = True
End Sub
End Class

Last edited by JohnRH; 14th Jul 2010 at 04:31 PM.. Reason: Updated Code
 
Reply With Quote
 
New Member
Join Date: Jan 2012
Posts: 2
 
      26th Jan 2012
The easiest method is just to use the Click event instead of the CheckChanged event..

AddHandler CB.Click, AddressOf CheckChanged_EventHandler


In the handler confirm that they wish to continue and then if they answer no


If CB.Checked = TrueThen
CB.CheckState = CheckState.Unchecked
Else
CB.CheckState = CheckState.Checked
EndIf
Exit Sub


 
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
CheckChanged event not firing when unchecking CheckBox in GridView jlficken@gmail.com Microsoft ASP .NET 1 12th Jul 2007 04:58 PM
stumped...table row click event and then cancel the checkbox click =?Utf-8?B?ZGF2ZQ==?= Microsoft Dot NET 1 2nd Sep 2006 03:53 AM
Can you cancel the click of a checkbox? =?Utf-8?B?Qnlyb24=?= Microsoft Dot NET Framework Forms 7 1st Jul 2005 12:56 PM
CheckBox - How to cancel a event? SQLNewbie Microsoft C# .NET 2 10th Mar 2004 03:09 PM
CheckBox CheckChanged Event Jim Heavey Microsoft ASP .NET 3 5th Mar 2004 06:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 AM.