I’m writing a .NET 4.0 WinForms app to configure a hardware device. For the sake of example, let’s say that the device has 10 settings that can be configured. The device can also store 5 different configurations. So I’ve created a configuration object that defines the 10 settings. In my form’s constructor, I’m creating an array of 5 configuration objects and setting that array as the datasource for a combobox. The user will select a configuration from a combobox (dropdownlist) and the settings for that configuration will be displayed in the UI. When the user selects a different configuration via the combobox, I need to make sure that all of the settings in the UI have been updated in the current configuration object before I display the settings for the newly selected configuration object. I’m currently doing this in the SelectedIndexChanged event handler for the combobox. When the user is done changing settings, they click a button which will write all five configurations to the hardware device. Here’s where the problem lies. If, for example, they select the third configuration, change some settings in the UI and then click the write button, the settings stored in the third configuration object haven’t been updated yet so the settings reflected in the UI will not be what gets written to the device. I’ve thought of a couple of ways to resolve this:
-
In the Click event for the write settings button, I could write new code that would always update the data in the appropriate configuration object with the current settings in the UI before writing the data for all five configurations to the device.
-
In the Click event for the write settings button, I could programmatically set the SelectedIndex value for the combobox to its current value thereby triggering the SelectedIndexChange event handler which would cause the settings to be written to the configuration object before being redisplayed. Then I could write all of the configurations to the device.
I think either would work with perhaps the biggest difference being writing new code for #1 vs. triggerring an existing event handler which already contains the functionality that I need for #2. The more I think about this, though, the more I wonder what is generally regarded as the “correct” way to do something like this. Does anyone have any thoughts or ideas on this? Thanks very much!
It sounds like you aren’t using a BindingSource and you might be doing more work than you need to do.
When using databinding in WinForms, you usually shouldn’t need to use most of the events you mentioned to accomplish typical requirements.
For a start, if you aren’t familiar with BindingSource, read up on that.
This is how you should have it set up:
In the forms designer, create a new DataSource from your Configuration class (the one with the 10 settings).
Add a BindingSource component to the form.
Set the DataSource property of the BindingSource to the new DataSource you just created.
Set the ComboBox DataSource to the BindingSource.
Set the ComboBox DisplayMember to the appropriate descriptive property of the BindingSource (which now represents the settings class).
Bind the Text properties of the 10 TextBoxes to the 10 settings properties of the BindingSource.
At runtime, set the DataSource property of the BindingSource to the array containing the 5 configuration objects.
At this stage you have working databinding. Use the UI to make changes to some of the settings and the changes will be reflected in the datasource (your array).
(If it doesn’t work or behaves strangely, you may need to remove the event handling you’re currently doing.)
Note that you do not need to write ANY code so that changes that the user makes are written into your datasource. That happens automatically. You just need to write the code to persist the data somewhere.
By default, each setting change will only be reflected in the array when the textbox loses focus and another control which has CausesValidation = true gains focus. So if your “write settings” button has CausesValidation = true (which is the default), then your array will contain all the up-to-date data when you handle that click event.
If you prefer you can have any changes made to the settings textboxes reflected immediately in the datasource by changing the UpdateMode of the bindings to OnPropertyChanged. The default is OnValidation, which means not until the the focus moves to another control with CausesValidation = true.
One more thing and then I’m going to stop for now (because I know I might be telling you everything you already know):
Consider making your Settings class implement INotifyPropertyChanged. You can then handle the PropertyChanged event to perform validation as the user enters changes. Alternatively you can handle the Validating events on the controls.
If you already know all of the above then I apologize for making the wrong assumption. From your question I get the impression that you haven’t fully discovered WinForms databinding yet.