tags:

views:

173

answers:

1

The ToolStripManager is hopelessly broken. LoadSettings doesn't do a thing...and I'm evidently not the only one with this problem:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/656f5332-610d-42c3-ae2d-0ffb84a74b34/

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=128042

So...anyone have a workaround? The one posted in that thread just moved all the toolbars to the bottom of the window.

A: 

Here is my solution:

Declare this struct to store the settings of a toolbar:

[Serializable]
public struct ToolStripSettings
{
    public bool Visible;
    public Point Location;
}

Code to save

// save toolbar settings
List<ToolStripSettings> toolSettings = new List<ToolStripSettings>();
// mToolbars is initialized in the constructor to contain all of your toolbar members
// you could also probably populate it with reflection
foreach (ToolStrip ts in mToolbars)
{
    ToolStripSettings tss = new ToolStripSettings();
    tss.Visible = ts.Visible;
    tss.Location = ts.Location;
    toolSettings.Add(tss);
}
// serialize the toolSettings list wherever you keep the 
// rest of your user-specific settings

Code to restore

// Load toolstrip settings, if any
if (/*deserialized storage location*/ != null)
{
    for (int i = 0; i < mToolbars.Length; i++)
    {
        mToolbars[i].Visible = /*deserialized storage location*/[i].Visible;
        mToolbars[i].Location = /*deserialized storage location*/[i].Location;
    }
}
Nick
Is restoring the Location all that is necessary if the ToolStrip was moved to a different ToolStripPanel? Ie, you don't have to explicitly store the parent container?
Not Sure
You are right. You need to store the parent name as well--I'll update my post once I've fixed this.
Nick