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
Related
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
Ive been working on a C# project for a while now and I fell it needs change its look.
I have found a UI kit that I really like, which can be seen below:
http://medialoot.com/item/transparent-ui-kit/
Does anyone know how to do this?
Thanks
EDIT: Im using WinForms
EDIT2: Maybe I should convert over to WPF? Is this doable?
You can subclass most common controls and draw their appearance yourself. However, unless for novelty applications I doubt your users will thank you for doing so.
Your example is a library of controls. For you to have an appearance such as that you'll have to create or acquire a similar library of controls and replace all your controls in your project to get that appearance.
If you want to update colors (background, foreground, etc.) and such you can make a class that recurses through controls and sets the settings using reflection and a switch statement to process each control. Then just run this on each form before you show it. One word of caution about this, some controls don't respect your changes and get overridden with themes from the OS (datetimerpicker being the biggest culprit). You'll also need to consider whether your users will appreciate the extra work put in for color/appearance changes.
If you don't have very good design skill and have a good know how about creating such templates, its better to buy them.
And C# is just a language. You're looking to create templates and skins for either for ASP.NET website/application or for windows forms.
Have a look at DevExpress Skins
If you can afford it.
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.
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.
First of all, I hope I'd get some advice about my practice because based on the very few books I've read, they didn't write much in the aspx page..they just built some controls and used them in the aspx page, so is this approach a good practice ?
here comes my question, I thought using web controls instead of directly writing into .aspx page is better as I could reuse the code, but now I'm creating those controls and I don't think I'll reuse them again or maybe only just one more time.
so do you think it's wise to create a control for the code instead of directly coding in the .aspx page ?
I was also working on a web user control for adding a new item to my db, and then I started planing for the update or edit control..I thought maybe I'd use the same control for both add and edit and start reusing my code, and on my way editing the control to be able to function as both add and edit control, I started with adding properties to the control, then a couple assignments in the Load method, then some checks with if...So I realized maybe a new control would be better!
I don't know, I'm thinking intuitively but I could really use a professional, experienced point of view.
Thanks for your time =)
Sometimes creating a user control, allows you to encapsulate some specific logic and ui elements into a separate class. Even if you are not reusing the control, the final code may be simpler to read and maintain. Take by example a Login control, if you take login related decisions in the user control and make those 'details' hidden in the rest of your code, then the code get simpler and easier to read and mantain!
If you're not going to reuse the code, then you don't want a user control or any other kind of control. Just put the appropriate code and controls onto the page.
If you find later that you do want to reuse it, then you can make a user control out of it.
If you know for sure that you are going to want to use a control (or some slight variation) then creating the user control is a no brainer.
For me, if it occurs to me that I may need similar functionality again in some future project then I will sometimes create a control just because I think it will be useful.