DavidN
DavidN

Reputation: 111

Save/load controls made inside C# windows forms

i'm developing a program where the user adds controls such as a picturebox, label etc. into a groupbox, i want to be able to save his the work to a file so he can open it later, i've only worked with xml before, and i want to know your opinion, do you think saving the controls into xml file is a good way to solve this? Or perhaps pass all the data through a streamwriter by looping all controls? Or another solution, i want to know what you recommend in this case.

thanks in advance, DavidN

Upvotes: 0

Views: 1166

Answers (2)

VhsPiceros
VhsPiceros

Reputation: 426

if I need take the decision,, I will try implement xml , devExpress use xml for this purpose. look the follow link

http://documentation.devexpress.com/#WindowsForms/CustomDocument4879

The example is very easy to use, but I don't know if very difficult to implement.

navBarControl1.SaveToXml("c:\\NavBarLayout.xml");
navBarControl2.RestoreFromXml("c:\\NavBarLayout.xml");

Upvotes: 1

CodeCamper
CodeCamper

Reputation: 6984

They say not to reinvent the wheel but you could do this a countless number of ways. It really depends on the style, format, and audience of the user. Sometimes I like to do everything in a simple txt style file this way anyone can easily edit the information. XML will offer you the ability to more easily organize your information which probably will be better. It really is up to you, I would say whatever you are more comfortable with. I personally would make my own little algorithm to search a plain txt file but that is because I am not using XML.

You should create a Class just for handling saving and loading the file.

Something like

       public class CustomControls
    {
        //more code
        public void AddCustomControl(Control x) { }
        public void RemoveCustomControl(Control x) { }
        public void UndoControl() { }
        public void RedoControl() { }
        public void SaveControls(string file){}
        public void LoadControls(string file){}
        //more code
    }

I think after you start working on your Class that handles the saving and loading you will have a much clearer idea of what will make it easier for you and the end user.

*You will also notice that you may want to keep track of the Custom Controls added via your theoretical CustomControls Class otherwise you will have to pass them somehow. I wouldn't recommend looping through all the form controls but rather keep track of what you added dynamically via the CustomControls Class. This will also enable you to implement features such as undo and redo.

Upvotes: 1

Related Questions