Windows Develop Bookmark and Share   
 index > Windows Forms Designer > proper implementation of IPersistComponentSettings
 

proper implementation of IPersistComponentSettings

hi,

i need to store some default settings for few of my components. Those shall be stored for the user and should survive app restart. So i believed that Settings for my winform app would be the right place. Unfortunatelly, it seems not as serializer allways throws an exception because of presence of Site property.

So - is there a way to filter out some props so that they wouldn't be persisted or ifIPersistComponentSettings implementation is the right solution - do you know of any exapmle of its implementation?

thanks a lot for any hint!
L
LubosSykora  Monday, August 17, 2009 1:17 PM

Hi LubosSykora,

Yes, we can have the control implement IPersistComponentSettings interface to support custom persisting. Before that we need to create our own setting class inherits from ApplicationSettingBase which contains all the setting related to some properties of the control. This is a sample:

//Custom setting used to persist controls.
//We can add properties that we want to persist.
public class MyControlSetting : ApplicationSettingsBase
{
    //A setting item, its scope is user, its default value is blank.
    [UserScopedSetting()]
    [DefaultSettingValue("")]
    public string TextBoxText
    {
        get
        {
            return this["TextBoxText"].ToString();
        }
        set
        {
            this["TextBoxText"] = value as string;
        }
    }
}
//A custom control supports custom persisting via MyControlSetting
//Commet: If we add this control via designer, the LoadComponentSettings method would be called automatically
//Commet: If we add this control during runtime, we need to call it programmatically when the control is initialized.
//Commet: We need to override the Dispose method to call the SaveComponentSettings method to persist the setting.
public class MyPersistControl : UserControl, IPersistComponentSettings 
{
    private TextBox _innerTextBox = null;
    public MyPersistControl()
    {
        _innerTextBox = new TextBox();
        _innerTextBox.Location = new Point(5, 5);
        this.Controls.Add(_innerTextBox);
        _setting.SettingsKey = "Name";
    }
    //A property related to the TextBoxText setting.
    public string TextBoxText
    {
        get { return _innerTextBox.Text; }
        set { _innerTextBox.Text = value; }
    }
    #region IPersistComponentSettings Members
    //The setting class we used to do custom persisting.
    private MyControlSetting _setting = new MyControlSetting();
    private bool _saveSettings = true;

    //Load settings.
    public void LoadComponentSettings()
    {
        this.DataBindings.Add("TextBoxText", _setting, "TextBoxText");
        _setting.Reload();
    }
    //Reset settings.
    public void ResetComponentSettings()
    {
        _setting.Reset();
    }
    //Save settings.
    public void SaveComponentSettings()
    {
        _setting.Save();
    }
    //If automatically save settings.
    public bool SaveSettings
    {
        get
        {
            return _saveSettings;
        }
        set
        {
            _saveSettings = value;
        }
    }
    //Setting key, we use 'Name' because the names of the controls would be different from each other.
    public string SettingsKey
    {
        get
        {
            return _setting.SettingsKey;
        }
        set
        {
            _setting.SettingsKey = value;
        }
    }
    #endregion
    //Override the Dispose method to save the setting.
    protected override void Dispose(bool disposing)
    {
        this.SaveComponentSettings();
        base.Dispose(disposing);
    }
}

You can get more information about Application Setting for Custom Controls from:
http://msdn.microsoft.com/en-us/library/6a0381s6.aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Aland Li  Wednesday, August 19, 2009 8:29 AM

Hi LubosSykora,

Yes, we can have the control implement IPersistComponentSettings interface to support custom persisting. Before that we need to create our own setting class inherits from ApplicationSettingBase which contains all the setting related to some properties of the control. This is a sample:

//Custom setting used to persist controls.
//We can add properties that we want to persist.
public class MyControlSetting : ApplicationSettingsBase
{
    //A setting item, its scope is user, its default value is blank.
    [UserScopedSetting()]
    [DefaultSettingValue("")]
    public string TextBoxText
    {
        get
        {
            return this["TextBoxText"].ToString();
        }
        set
        {
            this["TextBoxText"] = value as string;
        }
    }
}
//A custom control supports custom persisting via MyControlSetting
//Commet: If we add this control via designer, the LoadComponentSettings method would be called automatically
//Commet: If we add this control during runtime, we need to call it programmatically when the control is initialized.
//Commet: We need to override the Dispose method to call the SaveComponentSettings method to persist the setting.
public class MyPersistControl : UserControl, IPersistComponentSettings 
{
    private TextBox _innerTextBox = null;
    public MyPersistControl()
    {
        _innerTextBox = new TextBox();
        _innerTextBox.Location = new Point(5, 5);
        this.Controls.Add(_innerTextBox);
        _setting.SettingsKey = "Name";
    }
    //A property related to the TextBoxText setting.
    public string TextBoxText
    {
        get { return _innerTextBox.Text; }
        set { _innerTextBox.Text = value; }
    }
    #region IPersistComponentSettings Members
    //The setting class we used to do custom persisting.
    private MyControlSetting _setting = new MyControlSetting();
    private bool _saveSettings = true;

    //Load settings.
    public void LoadComponentSettings()
    {
        this.DataBindings.Add("TextBoxText", _setting, "TextBoxText");
        _setting.Reload();
    }
    //Reset settings.
    public void ResetComponentSettings()
    {
        _setting.Reset();
    }
    //Save settings.
    public void SaveComponentSettings()
    {
        _setting.Save();
    }
    //If automatically save settings.
    public bool SaveSettings
    {
        get
        {
            return _saveSettings;
        }
        set
        {
            _saveSettings = value;
        }
    }
    //Setting key, we use 'Name' because the names of the controls would be different from each other.
    public string SettingsKey
    {
        get
        {
            return _setting.SettingsKey;
        }
        set
        {
            _setting.SettingsKey = value;
        }
    }
    #endregion
    //Override the Dispose method to save the setting.
    protected override void Dispose(bool disposing)
    {
        this.SaveComponentSettings();
        base.Dispose(disposing);
    }
}

You can get more information about Application Setting for Custom Controls from:
http://msdn.microsoft.com/en-us/library/6a0381s6.aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Aland Li  Wednesday, August 19, 2009 8:29 AM

You can use google to search for other answers

Custom Search

More Threads

• How to manually turn-on Language Input?
• VB 2008 The designer loader did not provide a root component but has not indicated why
• Exception has been thrown by the target of an invocation. While using DesignerHost
• Moving from Java to C#
• Binding to Array
• How to change the bitmap of a component in the designer
• Subscribe to UserControl's ParentForm events
• Unable to display Form Designer or build any projects.
• unable to open any windows form in the windows designer.(Make sure the application for the file type(.cs) is installed)
• PropertyGrid with a Generic Collection