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.
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!