how to cancel checkChanged or click event for a checkbox?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 
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?
 
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.
 
Cancel Checkbox CheckChange Event

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:
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 = True Then
CB.CheckState = CheckState.Unchecked
Else
CB.CheckState = CheckState.Checked
End If
Exit Sub


 
Back
Top