1

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 ?

8
  • 2
    I would serialise the controls to JSON and store the resulting text. This should get you started: stackoverflow.com/questions/15843446/… Commented Sep 15, 2015 at 8:38
  • KISS: you could also save the data that created the controls and reprocess? Commented Sep 15, 2015 at 8:41
  • possible duplicate of How I can save controls created in run time in Windows Forms Commented Sep 15, 2015 at 8:44
  • So there's a relationship between your created controls and "some data passed", is storing those data easier ? Commented Sep 15, 2015 at 8:49
  • @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. Commented Sep 15, 2015 at 8:50

1 Answer 1

2

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.