Save the state of a dynamic windows form application C# - c#

I have built a form where the user can add controls, buttons and more dynamically.
is it possible somehow to save the dynamic controls and load them automatically the next time the user starts the program?
if it is not possible I would like to know if there is a common method or good practice for saving this data to some kind of a config file?
Thanks

One way would be to loop through all the controls on the form and save them to Isolated Storage...
https://msdn.microsoft.com/en-us/library/3ak841sy.aspx
https://msdn.microsoft.com/en-us/library/xf96a1wz(v=vs.110).aspx
But it really depends on your use case, do you want to do this automatically and transparent from the user, or do you want to present the user an option to save to a file somewhere?
While it is not specifically for dynamic controls, to give you more ideas see link below on another stackoverflow question :
Best practice to save application settings in a Windows Forms Application

Related

Changing application colours/theme by user preference

I am working on on a winforms C# application and want to give the user an option to change the colour of the form background and text throughout the application.
The application contains panels, labels, textboxes etc.
One way I thought of was to store the user preference as the colours RGB value and have the form set it when the form loads for all the various controls. But this way seems a bit laboured.
Is there an easier or more effective way of achieving this?
In my opinion, the easier way to do this is to use settings file in winform which will keep user data in a file and recalls it at anytime. You can follow this link to implement this demo
You only have to create one class that handles the theme. You can automate it.

Multiple Resources in one project

I want to create one project with different versions like
Normal
Purchased
So as per my version I want to change some detail of my project like if someone is using Normal then icon should be "Normal.png" and if my it's Purchased then all the form should have icon of "Purchased.png".
Currently what happens is I wrote a code on condition base in every form.
Is there any way from where I can avoid this?
Is there any way from which I can create a new resource file and during compiling time tool will decide which resource file to use?
I would create a static "theme" class that you can call when loading a windows form--and then pass it the current form. Depending on the current theme (in this case, "Normal" or "Purchased"), manipulate the form accordingly.
See this link for an example of changing a form's icon dynamically with code using resources.
You can also bind properties of controls to application settings -- see this link for more on that. (This wouldn't apply for the form icon, but it could help with other changes you want to make based on Normal/Purchased behavior).

How to develop my own .NET controls?

I've been struggling for a long time with basic controls that Windows Forms offers to developers, but... right now, I am developing an application that requires more advanced control than normal "TextBox".
Since, at this time, my application is about memory management, I have to show in the form, the process memory in bytes (or other type of data) to the user, giving it the ability to modify it as he wants.
The problem comes here, because... if I show the data in a TextBox, it only allow me to display the data in read-only text because if I let the user modify the textbox directly, it will be very messy and unaesthetic.
I was reviewing some projects on SourceForge about C# and the handling of hexadecimal data, and i found a good project, called Be.HexEditor, which has a control developed and designed by its creators, but in GDI+.
The control is called HexBox, and that's just what I need to get.
Do any of you know how to develop a control like this?... I would greatly facilitate things. What kind of manuals/books should I read to learn this kind of development? I ask this because I ignore everything about GDI+.
Or... is there other way for do it?
I would use a textbox to show to the user the current value and another textbox to enter the desired value (maybe a slider will work better).
I think you'll find it's GDI+ that you have to learn
http://www.amazon.co.uk/GDI-Programming-Creating-Controls-Programmer/dp/1861006314
You may still be able to write controls in WPF and then include them somehow into your winforms app, but if you need to push pixels GDI+ is the only way.

Save/Load User Settings on multiple UserControls within a Window

This post is related to c# - approach for saving user settings in a WPF application?
I have found multiple examples for saving/loading one window configuration but I am not sure if it will also save the configurations for all usercontrols open inside that window or just the actual main windows configuration...
First is it possible to save/load multiple usercontrol configurations inside a window using the application/user settings?
Am I forced to just read/write xml to store the the multiple usercontrols configuration?
Thanks in advance and let me know if you need any other information!
EDIT
I have an application with a ribbonbar on top that allows the user to open one or more user controls from the ribbonbar. Hypothetically if i wanted to save all the open usercontrols window position, height, width, etc... would it still be possible to use the application settings to accomplish this?
EDIT2
I have an application and within that application there are many usercontrols a user can access. If a user opens 2 usercontrols and uses those controls often, I want to be able to save those usercontrols to the workspace so there always there when the program is open or closed. The way I am approaching it now is with serialized xml from the database. I was researching this topic and came across the application settings approach and wanted to know if this was a viable approach for my situation. Thanks again Marc for taking the time to help me figure this problem out.
What I've used is user-specific ApplicationSettings and binding those to properties I want to 'save'. Here's a good example of what I've done http://www.jaylee.org/post/2007/02/WPF-DataBinding-and-Application-Settings.aspx
I little more detail might be helpful here, I'm not sure I follow. Are you saying you have multiple instances of a given user control on a single window and you want to persist the properties for each of them?
A lot of this depends on how you created and populated those controls. If they were created dynamically from data for example, the best solution is to serialize that data to disk and re-create the controls when the application restarts.
Alternatively you could bake in the serialization of the settings into the user control itself such that it writes out it's settings to a file name based on it's instance name (control1.xml, control2.xml, etc) and then have it repopulate when the instance comes back to life.
Knowing how these controls are added and the properties are set in first place would be helpful.

Serializing MDI Winforms for persistency

basically my project is an MDI Winform application where a user can customize the interface by adding various controls and changing the layout. I would like to be able to save the state of the application for each user.
I have done quite a bit of searching and found these:
How to auto save and auto load all properties in winforms C#?
Save WinForm or Controls to File
Basically from what I understand, the best approach is to serialize the data to XML, however winform controls are not serializable, so I would have use surrogate classes:
http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx
Now, do I need to write a surrogate class for each of my controls? I would need to write some sort of a recursive algorithm to save all my controls, what is the best approach to do accomplish that? How would I then restore all the windows, should I use the memento design pattern for that? If I want to implement multiple users later, should I use Nhibernate to store all the object data in a database? I am still trying to wrap my head around the problem and if anyone has any experience or advice I would greatly appreciate it, thanks
You don't want to serialize the actual control instances. They should be created and destroyed along with the Form they reside in. Rather look at what you let the user customize. Layout and position? Very well, save out the Top and Left coordinates for each control along with a control identifier. Do you let the user add new controls? Save their ids along with a type identifier so when its time to reload you are able to recreate the controls at their previous position.
Whether you use XML or some other format, there is no best approach or best practice, choose what makes sense for your project. XML happens to be an easy to go with format with great support in the .Net Framework.
I know there is a software, LinsUI Layout Manager, which handle your problems very well. They have free version for interested developers. You can check the site.
Cheer

Categories

Resources