[RESOLVED] ComboBox Validating does not fire after selection+edit
I'm working on a Windows Forms (.Net Fx 2.0) application.
I've got a standard WinForms ComboBox control, which has a hardcoded list of items. The user should also be able to type custom values, which are not in the list.
I need an (or multiple) event that fires when the value of the ComboBox was changed and the user leaves the control.
Currently I handle the SelectedValueChanged for the selection of a predefined item from the list. I also handle the Validating event for custom text typed into the ComboBox.
The above works if the user only selects from the list or only types a custom value.
However, if the user first selects a value from the list (SelectedValueChanged fires) and then edits the selected value without leaving the control, the Validating event is not fired when the user leaves the control.
Is there any event that fires in that situation?
Do I need to set a property of the ComboBox to make the Validating event fire?
Re: ComboBox Validating does not fire after selection+edit
The Validating even of any control isn't raised until the user tries to shift focus to another control. The point of the event is to validate the contents of the control and prevent the control losing focus if it fails, so why would it be raised any other time? You can also force the event to be raised by calling ValidateChildren on the form, but the point of that is to validate controls that have never received focus.
In your case, you'd have to handle some other event in order to force the Validating event to be raised anyway, so why wouldn't the other event be enough? If the user is typing directly into the control then you could handle the TextChanged event but how can you know that the user has finished typing? The only way to know for sure is when the user shifts focus to another control, which is why Validating should be enough. What is it that you actually want to do on that event?
Re: ComboBox Validating does not fire after selection+edit
Quote:
Originally Posted by
jmcilhinney
The only way to know for sure is when the user shifts focus to another control, which is why Validating should be enough.
Exactly! But the Validating event does not fire when the user leaves the control after first selecting and then editing the selected value.
(I edited my original post to clarify this part of the problem.)
Quote:
Originally Posted by
jmcilhinney
What is it that you actually want to do on that event?
I need to adjust some properties of other control depending on the value the user entered. - I think this is of minor importance for the question.
Re: ComboBox Validating does not fire after selection+edit
Hold on! - This might be a problem of my own making.
I just created a super simple demo from scratch to isolate the problem and there the issue is not reproducible; i.e. the Validating event fires as expected.
It must be something else in my form that prevents the Validating event. - I'll investigate.
Re: [RESOLVED] ComboBox Validating does not fire after selection+edit
I cannot pinpoint the exact cause, but there is quite a lot happening in the form when the ComboBox is updated. This includes removing/adding some other controls to the panel the ComboBox sits on, which I suspect to be the cause of the problem. - This is triggered in the problematic scenario by the SelectedValueChanged, which is fired first.
As it would require too much effort to further investigate the root cause, I now resorted to storing the "old value" of the ComboBox in SelectedValueChanged, then comparing that to the current value in LostFocus, and triggering my responses to a value change if required.
This workaround is not ideal, as it limits my options to respond to invalid input, but for now it is "good enough".
Thank you @jmcilhinney for your comment. It helped me to recognize that my problem is not standard behavior and needs a custom solution tailored to my specific scenario.