Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save user location of toolstrip still not working in VS2012

I've been struggling with multiple toolstrips in a toolstripcontainer for years. I have a current .NETv3.5 application written in VS2008 where the location of the toolstrips are random. A lot of custom code have been made to fix this but without any luck.

Currently I'm working in VS2012 on a .NETv4.5 application which also has multiple toolstrips. I created a very small test application that is a form with a docked toolstripcontainer and 4 toolstrips. At design time I added 3 toolstrips to the bottom and 1 to the right.

Without adding any code and starting this application, my toolstrips are located at the same location as in design time. Now I reorder the 3 top toolstrips and restart the application. My reorder changes are not saved the toolstrips are again as in design time.

Now I add these lines of code:

  public partial class Toolstrips : Form
  {
    private string keyName;

    public Toolstrips()
    {
      this.InitializeComponent();
      this.keyName = Application.ProductName + this.Name + "xyz";
      ToolStripManager.LoadSettings(this, this.keyName);
    }

    private void Toolstrips_FormClosing(object sender, FormClosingEventArgs e)
    {
      ToolStripManager.SaveSettings(this, this.keyName);
    }
 }

And still in VS2102 with .NETv4.5 this is not working. I can reorder what I want after restarting the application all toolstrips are restored to the design time locations.

I had hoped this would be fixed in VS2012 but apparently not. Does anybody have a work around to get this to work?

EDIT: Added contents user.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable>
            <setting name="ItemOrder" serializeAs="String">
                <value>toolStripButton4</value>
            </setting>
            <setting name="IsDefault" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>29, 42</value>
            </setting>
            <setting name="ToolStripPanelName" serializeAs="String">
                <value>toolStripContainer1.Right</value>
            </setting>
            <setting name="Visible" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="Location" serializeAs="String">
                <value>0, 336</value>
            </setting>
            <setting name="Name" serializeAs="String">
                <value>toolStripTable</value>
            </setting>
        </System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable>
        <System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan>
            <setting name="ItemOrder" serializeAs="String">
                <value>toolStripButton3</value>
            </setting>
            <setting name="IsDefault" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>40, 31</value>
            </setting>
            <setting name="ToolStripPanelName" serializeAs="String">
                <value>toolStripContainer2.Top</value>
            </setting>
            <setting name="Visible" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Location" serializeAs="String">
                <value>288, 0</value>
            </setting>
            <setting name="Name" serializeAs="String">
                <value>toolStripPan</value>
            </setting>
        </System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan>
        <System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard>
            <setting name="ItemOrder" serializeAs="String">
                <value>newToolStripButton,openToolStripButton,saveToolStripButton,printToolStripButton,toolStripSeparator,cutToolStripButton,copyToolStripButton,pasteToolStripButton,toolStripSeparator1,helpToolStripButton</value>
            </setting>
            <setting name="IsDefault" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>248, 31</value>
            </setting>
            <setting name="ToolStripPanelName" serializeAs="String">
                <value>toolStripContainer2.Top</value>
            </setting>
            <setting name="Visible" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Location" serializeAs="String">
                <value>6, 0</value>
            </setting>
            <setting name="Name" serializeAs="String">
                <value>toolStripStandard</value>
            </setting>
        </System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard>
        <System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom>
            <setting name="ItemOrder" serializeAs="String">
                <value>toolStripButton1,toolStripButton2</value>
            </setting>
            <setting name="IsDefault" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Size" serializeAs="String">
                <value>29, 73</value>
            </setting>
            <setting name="ToolStripPanelName" serializeAs="String">
                <value>toolStripContainer2.Right</value>
            </setting>
            <setting name="Visible" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Location" serializeAs="String">
                <value>0, 71</value>
            </setting>
            <setting name="Name" serializeAs="String">
                <value>toolStripZoom</value>
            </setting>
        </System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom>
    </userSettings>
</configuration>

EDIT2 I've created a small VS2012 application written in C# to illustrate my problem. You can download it here: https://mapwindow5.svn.codeplex.com/svn/tmp/ToolstripDemo/ToolstripDemo.zip When I start the application I see:

Initial load

When I reorder the 3 top toolstrips I get this:

After reorder

Now I close the application and reopen it again, I get the initial locations:

After reopen

Now I move the toolstrip which was docked at the right to the top:

Reorder again

I reopen the application and I see the 4th toolstrip is still at the top, which is OK but the order is not. Look at the two last toolstrips:

Reopen again

Another strange thing is that I cannot redock my zoom-toolstrip to the right again. The toolstrip just disappears:

enter image description here

When I open my form in VS2012 the toolstrips are rearranged as well: Design time

I'm not sure why. I'm just reopening the form.

I did my testing and made the screenshots by running the exe from the explorer, not in debug-mode of VS.

I hope it is now more clear what I mean and what I need. Any advice is very much appreciated.

like image 230
Paul Meems Avatar asked Sep 18 '13 09:09

Paul Meems


1 Answers

I use vb.net 2010, so I use vb's syntax, but i hope my answer helps:

1) Using ToolStripManager, You can save and load settings without specifying a key for all of toolbars seperately. Specifying a form is enough - settings will be saved for a form using form's name as a key.

2) It is important to call ToolStripManager.SaveSettings() and ToolStripManager.LoadSettings() not prepended by something else programaticaly changing toolbars layout at runtime in FormLoad and FormClosing event-run subroutines. So most safe way to do this, is just call the methods as first in FormClosing and FormLoad subs, as below:

Private Sub Explorer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ToolStripManager.LoadSettings(Me)'Me is the form, for which we save the toolbars settings

    'further code in this subroutine

End Sub

Private Sub Explorer1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    ToolStripManager.SaveSettings(Me)
    My.Settings.Save()

    'further code in this subroutine
End Sub
like image 130
Jasiek Avatar answered Oct 16 '22 15:10

Jasiek