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