1
votes

I create winform and its control created in run time based on some data passed to the form.
I don't know the number of controls will be created also the type of control.
The data passed to form just text and i make some condition to check create label or textbox or button.

I want to save this controls name, Location,Text. This controls can be textbox,button, label,ComboBox.

How can i make that ? if XmlSerializer can be valid in this case? if yes how can use it?
Can anyone give me a bit of code or link ?

1
I would serialise the controls to JSON and store the resulting text. This should get you started: stackoverflow.com/questions/15843446/…Ulric
KISS: you could also save the data that created the controls and reprocess?Falco Alexander
So there's a relationship between your created controls and "some data passed", is storing those data easier ?Nam Bình
@NamBình I don't know the number of controls will be created also the type of control. The data passed to form just text and i make some condition to check create label or textbox or button.Ahmed

1 Answers

2
votes

Controls aren't designed to be saved. so you can't do this with the controls themselves, but if you write a class that contains whatever details you need from the control then you can save and use them however you want. just mark them as serializable and feed them into a stream writer and reader (https://msdn.microsoft.com/en-gb/library/ms233843.aspx)

[Serializable]
class ControlFactory
{
    enum ControlType
    {
        TextBox
    }
    ControlType Type {get;set;}
    Point Position {get;set;}
    //etc.
    Control Create()
    {
        switch(Type)
        {
            case ControlType.TextBox:
                TextBox txt = new Textbox();
                // apply settings
                return txt;
        }
    }
}