I have a ComboBox bound to a table field and I want to validate the selection, since only certain selections are valid depending on context. When in the On Validating Event how can I get the Value before or what it is in the table.field before selection changed?
A:
The WinForms ComboBox control doesn't provide the functionality of being able to intercept a value change and cancel it. You can use the Validating
event (or, as you describe, subclass the ComboBox and override OnValidating
), but these only allow you to keep the focus on the control. You can't actually "cancel" a value change through the validating events.
There are several third-party (DevExpress, for example) packages that provide ComboBoxes that allow you to inspect both the initial and the new values when the value changes and cancel the change if desired, but the ComboBox provided in System.Windows.Forms does not.
Adam Robinson
2010-01-21 15:22:31
Thanks.I had a quick look at their site. Does cost some $. But my biggest concern is not knowing what I might be missing with their Combo as oppose to the one in VS. Are you using DevExpress?
bochur1
2010-01-21 16:09:36
@bochur1: I don't use them where I am right now, but I used them extensively at my last employer. While the package can be pricey, the controls themselves are very high-quality. If all you're looking for is a ComboBox that you can cancel changes on then there are likely more economical options, but as WinForms control libraries go, the DXperience package is one of the (if not THE) best.
Adam Robinson
2010-01-21 16:16:55
>If all you're looking for is a ComboBox that you can cancel changesDo they allow for multiple column in the ComboBox?Otherwise, all I'm looking for is the Cancel. What other options do I have?
bochur1
2010-01-21 16:46:15
@bochur: The DevExpress ComoBox does allow for multiple columns. As for other options, I've never done research, but I would imagine that most control packages include such a combobox.
Adam Robinson
2010-01-21 16:49:16