winforms component for state persistence
I don't make very many winforms apps in my free time,
but for those who are making them, I have a little assembly that you may be
interested in.
It started when a user wanted the application to remember where he placed the
form and how big it was between starts of the app. A pretty reasonable request,
I thought.
I figured, I'd just create a state persistence component that I drop on the
base form class that all my app's forms derive from, and it'll be done. I
figured that this component has probably already been written somewhere.
What I found is that there weren't any that didn't require ( at least ) some
code to be written to get it to work. I wasn't happy with that, so I figured I'd
just write my own.
I started off just writing a component, but found that the Component class
doesn't have a way of automaticly knowing about the form it has been dragged
onto. This makes sense, as most components have nothing to do with forms. I
figured I could use Control, but I wanted to keep my component non-visual, and
living in the component tray in vstudio. What I wanted was a mid-level object
between Component and Control. Unfortunately, no such class exists. So I created
one.
Say hello to FormComponent. When dragged onto a windows form, it overrides
the normal InitializeComponent code-serialization process and adds a line of
code which sets the current form as the component's Form property. Once this
piece of magic is in place, components that want to access the current form
don't have to rely on the developer to write code hooking them up.
So now writing the state persister is trivial. I simply subscribe to some key
events, Load, Resize, Move, and Closing, to load, track, and save the state of
the form to xml in the user's app data folder.
anyway, enough jabbering, here's the link to the form state
persister and base FormComponent class, for easy creation of similar
form-oriented components. In the download is the full solution source code in
C#, a precompiled version for those who just want to use it, and an example
application
with 1 form that persists its state.
I hope yall like it, have fun.