By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

how to cancel checkChanged or click event for a checkbox?

P: n/a
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
Aug 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Aug 7, 12:22 pm, Rich <R...@discussions.microsoft.comwrote:
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

Aug 7 '07 #2

P: n/a
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.comwrote:
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

Aug 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.