Reputation: 305
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 ?
Upvotes: 1
Views: 860
Reputation: 5500
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;
}
}
}
Upvotes: 2