Hi fellow developers,
I had to deal with one of those pesky situations where the 'Save' Button gets enabled in a Form although it shouldn't. The situation in which this happened for me was (for a change!) very easy, uncomplicated, yet it was very puzzling as to why this happened when the user didn't change a thing, and no program code changed a thing, too (this is Bug https://tracker.openpetra.org/view.php?id=3992)!
Here's the particular situation (note: all of the below must be like this for this to happen):
- A Form or UserControl contains an 'OpenPetra'-type ComboBox (TCmbAutoComplete or TCmbVersatile or TCmbAutoPopulated) and this ComboBox isn't tied to a Column in a DB Table (i.e. it is 'unbound').
- The ComboBox has got an 'empty' Item as its first item and this is what gets shown when the Form / UserControl is opened (that is, in the YAML file its 'OptionalValue' Elements' value is set up like this: OptionalValues: [=,XXX, YYY]).
- The Form / UserControl is brought up at run time.
- The Save Button isn't enabled.
- The user clicks into the ComboBox or gets into the ComboBox by 'tabbing' into it.
- The user leaves the ComboBox (by clicking into another Control or by 'tabbing' out of it).
In that situation the 'Save' Button gets enabled and it took me quite some time to find out as to why this happens!
Here's the reason, and how to prevent this from happening:
1) When the Form or UserControl is first initialised the Method 'TFrmPetraEditUtils.ClearControls' runs automatically. For any 'OpenPetra'-type ComboBox this code sets the ComboBoxes' SelectedIndex Property to -1 (meaning: no item selected).
2) When the user leaves the ComboBox, the 'OnLeave' Event code of the TCmbAutoComplete ComboBox runs. There the following assignment is run: 'this.SelectedIndex = mFoundIndex'. As 'this.SelectedIndex' is -1 but mFoundIndex is 0 (the Index of the 'empty' Item) the Save Button gets enabled because the SelectedIndexChanged Event of that ComboBox is raised and this hooked up automatically in all OpenPetra Forms to enable the Save Button!!!
3) Would 'this.SelectedIndex' be 0 and mFoundIndex be 0 then the assignment would still be made, but the SelectedIndexChanged Event would not be raised by WinForms.
4) This leads to the conclusion that ManualCode needs to be put in place that sets such a ComboBoxes' SelectedIndex from -1 to 0 when it should show the 'empty' Item at Form / UserControl initialisation (e.g. in a ShowDataManual Method).
It might be good to visit one of 'your' Forms in which you know there is an 'unbound' ComboBox that has an 'empty' value and it is 'empty' at Form / UserControl initialisation time - in that case check whether the 'Save' Button comes on when you leave that Control (chances are that it is!).