Cannot get IPersistComponentSettings to work
I cannot seem to get IPersistComponentSettings to work for a UserControl I created. For what I read (actually, what little there IS to read), all I need to do is the following:
- Create class that inherits from UserControl. (Let's call it MyUserControl)
- Create settings class that inherits from ApplicationSettingsBase. (Let's call this MyControlSettings)
- Set MyUserControl to implement IPersistComponentSettings
- Modify the Dispose method of MyUserControl to call SaveComponetSettings method of IPersistComponentSettings
- Have the SaveComponentSettings method call the Save method of MyControlSettings
- Drop MyUserControl onto a Form
Now, according to the documentation, at some point (it does not say when) the LoadComponentSettings method of IPersistComponentSettings is supposed to be called. I cannot seem to get this to happen. The documentation seems to imply that this method will be called for me.
Now, when my dispose method gets called, the code does eventually call the Save method on the settings class. But the settings do not look like they are getting written to the file found under the "Application Data" folder.
On the positive side, it does look like something is trying to set the property SaveSettings for IPersistComponentSettings. The problem is that it is setting it to false. (And nothing seems to try and GET the value.)
Does ANYBODY have a simple example that they can post or point me to? Even an article that does more than list methods. the MSDN docs seem completely useless...
http://msdn2.microsoft.com/en-us/library/6a0381s6(d=ide).aspx
http://msdn2.microsoft.com/en-us/library/6a0381s6(d=ide).aspx
Thanks.
- Jason
Well, I got LoadComponentSettings to be called. Seems you have to manually set the SaveSettings property to True at design time. When you do this, the designer adds the call to LoadComponentSettings. (You would think the docs would mention this little detail...)
But I still cannot get the settings to actually save. I am using a custom settings class for the form. When I call that save method, the settings get saved. But when within the control, no joy. Wonder if it is because I am making the Save call after my control's dispose method is called (which is I guess after the form is closed).... (But that is what the docs say I should do.)
Interesting enough, I found a blog entry which mentions that the ToolStrip control uses IPersistsComponentSettings. But then after further reading it seems they changed that. (This does not leave me warm, fuzzy feelings when Microsoft decides NOT to use their own class/interface...But then goes on to say that this is the "recommended" approach that should be used.)
Does anybody have any example of IPersistComponentSettings working? Anybody know if there are any framework classes using it? (I could use Reflector to see how it is being done.)
Thanks.
Well, I THINK I have gotten it to work. Although I really have no idea if I did this correctly. (And I had to ignore some of the points in the little docs MS provides -- could not make heads or tails of it...)
But here is another issue -- The settings that get saved are not nested. In order words, if I have a control on a form, the settings for the control and the settings for the form are saved at the same node level in the config file. You can see this here:
<userSettings>
<SettingsTest.MySettings.Form1>
<setting name="TheColor" serializeAs="String">
<value>Bisque</value>
</setting>
</SettingsTest.MySettings.Form1>
<SettingsTest.MyControlSettings.MyControl1>
<setting name="ButtonMessage" serializeAs="String">
<value>Errrrr</value>
</setting>
</SettingsTest.MyControlSettings.MyControl1>
</userSettings>
Now, the problem with this happens when I create two instances of the form...
While I can differentiate the different instances (hence different settings) of the form by setting the Name property, I cannot do the same for the control. (Unless of course I want to expose the Name property of the hosted control as a public property of the Form... but that would be silly....) As you can see below, there are two settings sections for each form, but only one for the control (when there should be two).
<userSettings>
<SettingsTest.MySettings.Form1>
<setting name="TheColor" serializeAs="String">
<value>Bisque</value>
</setting>
</SettingsTest.MySettings.Form1>
<SettingsTest.MyControlSettings.MyControl1>
<setting name="ButtonMessage" serializeAs="String">
<value>Errrrr</value>
</setting>
</SettingsTest.MyControlSettings.MyControl1>
<SettingsTest.MySettings.dlg>
<setting name="TheColor" serializeAs="String">
<value>Bisque</value>
</setting>
</SettingsTest.MySettings.dlg>
</userSettings>
Any thoughts? This is the four post regarding this interface I have made in 3 days and not a single response... Is there ANY chance of getting one? Do any developers from MS browse through the forums once in a while?
First of all, sorry for my bad englisch.
Have you try this:
Settings Keys and Shared Settings
Some
controls can be used multiple times within the same form. Most of the
time, you will want these controls to persist their own individual
settings. With the SettingsKey property on IpersistComponentSettings, you can supply a unique string that acts to disambiguate multiple versions of a control on a form.
The simplest way to implement SettingsKey is to use the Name property of the control for the SettingsKey. When you load or save the control's settings, you pass the value of SettingsKey on to the SettingsKey property of the ApplicationSettingsBase
class. Application Settings uses this unique key when it persists the
user's settings to XML. The following code example shows how a <userSettings> section may look for an instance of a custom control named CustomControl1 that saves a setting for its Text property.
Any instances of a control that do not supply a value for SettingsKey will share the same settings.
Here the link to the original website:
SettingsKey