Share via


ToolStripManager.SaveSettings and UserControls

Ingas asks a great question:

In windows form I have a ToolStripContainer inside a control (inherided from UserControl). I allso have a ToolStripContainer on the main form. I am trying to do ToolStripManager.LoadSettings() (with ToolStripManager.SaveSettings allready done earlier) - supplying the main form. The toolbars on the main form restore their positions, as well as the toolbars that have been moved to the main form by the user. But the toolbars inside the control does not. Is there (or will there be) a method that is fit for the user control?

You can use the same SaveSettings and LoadSettings – the problem you’re most likely running into is that SaveSettings and LoadSettings require you to have unique names for all your ToolStripContainers and ToolStrips. If you add a UserControl to your project and drag and drop a ToolStripContainer, it’s very likely that it is named “ToolStripContainer1” – this may clash with the “ToolStripContainer1” that is on your form.

 

If you look at what the settings scribbles out (mine was in a hidden directory C:\Documents and Settings\USERNAME\Local Settings\Application Data\Microsoft\WindowsApplication50.vsho_Url_cuxemhukaqqokqln4jqkqtjcvnmlj1iq\1.0.0.0\user.config) [1] you’ll see that the ToolStripManager saves out information based on the names of the items.

 

If there are multiple ToolStrips or ToolStripPanels of the same name, the ToolStripManager will just use the first control it finds of the same name. The fix is to ensure you have unique names for *everything* on your form.

 

 

Here’s how I set up settings that work with a UserControl:

  • Create a new form. 
    • Drag & Drop a ToolStrip
    • Open the smart tag, choose Insert Standard Items
    • Open the smart tag, choose Embed in ToolStripContainer
    • Open the smart tag for the ToolStripContainer, choose Dock fill in container
  • Create a new user control in the project
    • Drag & Drop a ToolStrip
    • Click the “new item glinker” to add a few stock images
    • Open the smart tag, choose Embed in ToolStripContainer
    • Open the smart tag for the ToolStripContainer, choose Dock fill in container
  • Important step - rename UserControl1's controls to be unique!
    • Rename ToolStrip1 to UserControlToolStrip1
    • Rename ToolStripContainer to UserControlToolStripContainer1
  • Hook up settings
    • Go to the form and add handlers for the Load and FormClosing events.
    • Write this code

        private void Form1_Load(object sender, EventArgs e) {

            ToolStripManager.LoadSettings(this);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {

            ToolStripManager.SaveSettings(this);

        }

 

 

That’s great but I followed your advice and my ToolStrip disappeared!

 

How to rename your controls:

If you rename your toolstrip or your toolstrip panel and you run the application again, you may notice that your toolstrip disappears. If there is no matching container for your toolstrip, the ToolStripManager assumes that the panel it was in has disappeared and therefore it should not add it. 

 

The easiest way to write a “Reset” for your form is to save the settings before calling load…

 

        private void Form1_Load(object sender, EventArgs e) {

            ToolStripManager.SaveSettings(this,"reset");

            ToolStripManager.LoadSettings(this);

        }

        public void Reset() {

            ToolStripManager.LoadSettings(this, "reset");

        }

 

More information

 

[1] Showing the config file for purposes of illustration. One should not directly parse the settings file from disk as there are multiple storage mechanisms for settings.

ToolStripSettings.PNG

Comments

  • Anonymous
    April 12, 2006
    Не всё так просто с новыми панельками ToolStrip. Много возможностей и много вопр
  • Anonymous
    April 12, 2006
    Good! Now it works!

    At least most of the time ;)
  • Anonymous
    January 17, 2009
    PingBack from http://www.hilpers.com/1174355-toolstripcontainer