Cancel Event - Decent Work Around

puccij
05-18-2004, 07:19 AM
I have a combobox on a form. When the selected value changes I want to repopulate a number of iother values on the form, but first I want to check to see if the user wants to save changes.

The problem is that this often will require that the code cancel the click event which as far as I can tell is not possible. If you use the vailidating event the code only fires when the combo loses focus.

The only work around I have seen posted is to capture the old selected index and reset the value if a "cancel" event is needed.

I have another suggestion that seems fairly obvious. I am simply putting the cancel code in the validating event, but also adding code to the click event that selects the next c ontrol on my form. This has the effect of instantly triggerring the valding event.

It ain't pretty but it's getting the job done.




Private Sub ComboChange(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
If CheckDataSave then e.Cancel = true
End Sub


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
nextcontrol.Select
End Sub

Machaira
05-18-2004, 12:25 PM
Why not just do:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
CheckDataSave
nextcontrol.Select
End Sub

or some variation of this? If you're allowing the user to cancel the selection of the new selection in the CheckDataSave then you can do:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
If CheckDataSave Then nextcontrol.Select
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum