//
you're reading...
General

ToolStrip positions won’t save? Give them a name!

Tonight, I was working on an application using the ActiveReports designer control.

While I was working on it, I thought it would be nice to save the position of the ToolStrips for the user and reload them, since there are quite a few toolstrips to deal with.

Microsoft was nice enough to include a couple methods that do this for you as part of Windows Forms: ToolStripManager.SaveSettings(Form) and ToolStripManager.LoadSettings(Form).

Nice!  Just a few lines of code hooked to the Load and Closing events and voilà! Saving and loading of my ToolStrip positions!  I ran it once, to get those settings saved.  But then I ran it again, and all my ToolStrips disappeared!  WTF?!?

After beating my head on this rock for 30 minutes I was finally able to piece everything together, after seeing that not exactly all of them disappeared…the StatusStrip I created in the designer was still showing up.

What sets my StatusStrip apart from the other ToolStrip derived items?   Well, pretty much the only difference between one created in the designer and a ToolStrip created through hand-written code is that I’m too lazy to set the Name property for something that doesn’t need it.

But it turns out you do if you want SaveSettings() and LoadSettings() to work for you!

Here is the anatomy of the bug as I could figure it out from the behavior. When you call LoadSettings it will look for a Application and User-specific config file, if it doesn’t find it then it does nothing and your app continues on its merry way.  When your application calls SaveSettings it will go through and start saving the ToolStrip information to that same config file, but; the way it stores the information is by using the names of the ToolStrips.

If the ToolStrip doesn’t have a name, it skips it and doesn’t write it to the file.

The next time you load your app and LoadSettings gets called it will read that config file, and go about setting your ToolStrips up.  But, here’s the kicker…any ToolStrips that exist in your application but are not listed in that config file (including ones with names) will be hidden!

Now I’ve gone through and given names to all my ToolStrips and ToolStripManager.SaveSettings/LoadSettings works great!  I’ve also entered case 168539 to have this fixed in ActiveReports 7.

tl;dr: calling LoadSettings will hide any ToolStrips that aren’t saved to the config file.  SaveSettings will only save ToolStrip information for strips that have the Name property assigned to them…so set the Name property on all of the ToolStrips you create through code so they get saved!

Advertisements

About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.

Discussion

One thought on “ToolStrip positions won’t save? Give them a name!

  1. Thanks, this was very helpful!

    Posted by Rafael Moura | December 2, 2016, 6:01 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archive

%d bloggers like this: