简体   繁体   中英

.net windows application remember form size and location

I have a windows application with hundreds of forms in it. I want to remember size, location and state of each for during user session so that when they launch it next time the size and location will be the same.

I see bunch of examples of doing this in the application settings, however since I have lots of forms I do not want to introduce big list of application settings. Along with it would be time consuming for me to do it for each form manually.

Do someone has a generic solution so that I can code at one place and that will do the job for all the forms in the applications?

Please advise

Have all the forms inherit the same base form and inside of it you implement the action of saving the state to an XML file, as well as reading and loading it. Eventhandlers on resizing should update the values and store to the xml files.

That's the only way I can see. You have to manage it yourself

I would use a DataSet with a specific DataTable for storing position and size of each window. Each window could be identified by a number or a text string, or something else unique. Maybe its name. Use the WriteXml and ReadXml methods of the DataTable to save/load to/from xml file.

There are of course many ways to implement it all. Inheritance (as suggested in an other answer) would probably be one of the neatest.

Another idea would be a class (let's just call it X) which keeps a collection (System.Collection.Generic.List for example) of all Windows. Windows (Forms) could be added to this collection by some "main" class, ie the class creating them. If that is not possible you could use the singleton design pattern, and let each window (form) gain access to the same instance of class X to add itself to the collection. This will also allow the class (X) to connect event handlers, to update its values when windows are moved, resized etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM