Is it possible to save a WinForms user control(ex: button) to the database? Or the only thing that can be saved is the settings property.
EDIT: Like what triggerX said. I tested the serializable idea.
<>
[Serializable()]
class btnAttrib
{
public Point LocationBTN { get; set; }
public Size SizeBTN { get; set; }
public string NameBTN { get; set; }
public btnAttrib(Point l, Size s, string n)
{
this.LocationBTN = l;
this.SizeBTN = s;
this.NameBTN = n;
}
}
<>
private void button2_Click(object sender, EventArgs e)
{
var btnAttr = new List<btnAttrib>();
btnAttr.Add(new btnAttrib(new Point(50, 100), new Size(50, 50), "Button 1"));
btnAttr.Add(new btnAttrib(new Point(100, 100), new Size(50, 50), "Button 2"));
btnAttr.Add(new btnAttrib(new Point(150, 100), new Size(50, 50), "Button 3"));
btnAttr.Add(new btnAttrib(new Point(200, 100), new Size(50, 50), "Button 4"));
try
{
using(Stream st = File.Open("btnSettings.bin", FileMode.Create)) {
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(st, btnAttr);
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Exception");
}
}
private void button1_Click(object sender, EventArgs e)
{
try {
using (Stream st = File.Open("btnSettings.bin", FileMode.Open)) {
BinaryFormatter bf = new BinaryFormatter();
var btnAttr2 = (List< btnAttrib>)bf.Deserialize(st);
foreach(btnAttrib btAtt in btnAttr2) {
Button nBTN = new Button();
nBTN.Location = btAtt.LocationBTN;
nBTN.Size = btAtt.SizeBTN;
nBTN.Name = btAtt.NameBTN;
this.Controls.Add(nBTN);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception");
}
}
这是节省用户控制的最佳决定因素吗?