Cancelling a user change in a combo box

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

Guest

I have a combobox control on the form. When the user makes a change, I want
to validate a condition before permitting the change. If the condition is
not met, I want to return the ComboBox text back to its previous value. He
is what I tried.

(1) Validating event does not work because user is not exiting the control.
(2) My condition testing code is in the SelectedIndexChanged event.
Unfortunately I can't find a way to recover the previous value if test fails.
(3) There is no .....changing event that I can use that supports e.cancel =
True. (4) My best thought is to set a class variable in the mouseDown event
to capture the current value and use that class variable in the
SelectedIndexChanged event if the condition test fails.

One would think there is an easier way than my #4 method but I can't find
it. Any alternative approach to this problem will be appreciated.
 
More specifics on the problem. My combobox contains a list of topics that
can be edited in an RichTextBox. If a person does not edit the topic, I want
the combobox to show a new topic from the list. If the user edits the topic,
I want to verify that the user saved the topic before changing to a different
topic.

In my question the test condition referred to whether the previous topic was
saved, not a question of a selection being invalid.
 
Genojoe,

I am not sure if I understand you, however a static variable in the index
change event seems for me the most on what you are asking.

\\\
Private sub selectedindexchange...............
static mysaver as string
'do all the stuff

mysaver = combo.text
end sub
///

I hope this helps,

Cor
 
Thank you. The single word "Static" solved the problem. Here is the
abridged code that I developed using a static variable. Note that I am using
the bSkipCode variable to keep this procedure out of an endless loop.

As an aside, if anyone reads this topic and has a better way to stop an
endless loop, I would love to learn about it. I frequently run into the
problem with needing to stop such a loop. Is using a static variable this
way the best way to do it. I am always looking for the cleanest way to solve
a problem.

Private Sub cboUser_SelectedIndexChanged(...........
Static iIndex As Integer
Static bSkipCode As Boolean
If bSkipCode = True Then
bSkipCode = False
Exit Sub
End If
If priTestForDirty(rtfUser) Then
MsgBox("Topic is not saved. You cannot move to another topic.")
bSkipCode = True
cboUser.SelectedIndex = iIndex
Else
iIndex = cboUser.SelectedIndex
End If
End Sub
 
Back
Top