如何存储用户控件以供将来编辑

时间:2014-08-31 15:45:06

标签: c# winforms user-controls

有人可以帮我解决一下 - 如何正确存储我的应用程序中以编程方式添加的控件数组?

更明确地说,我有一个以编程方式创建一些Labels和PictureBox的函数,并将其添加到Panel Control中。

var lbLip = new Label { Text = @"LIP :", Location = new Point(5, 20), Size = new Size(29, 13) };
var lbLipVar = new Label { Text = "", Location = new Point(32, 20), Size = new Size(79, 13) };

var lbRip = new Label { Text = @"RIP :", Location = new Point(5, 40), Size = new Size(31, 13) };
var lbRipVar = new Label { Text = "", Location = new Point(32, 40), Size = new Size(70, 13) };
var imgBox = new PictureBox { Image = Image.FromFile(@"data\icon\red.png"), Location = new Point(100, 40), Size = new Size(16, 11) };

var lbPid = new Label { Text = @"PID :", Location = new Point(5, 60), Size = new Size(31, 13) };
var lbPidVar = new Label { Text = "", Location = new Point(32, 60), Size = new Size(37, 13) };

var lbTime = new Label { Text = @"Time :", Location = new Point(80, 60), Size = new Size(39, 13) };
var lbTimeVar = new Label { Text = "", Location = new Point(115, 60), Size = new Size(49, 13) };

            var arrayLb = new Control[9];
            arrayLb[0] = lbLip;
            arrayLb[1] = lbLipVar;
            arrayLb[2] = lbRip;
            arrayLb[3] = lbRipVar;
            arrayLb[4] = imgBox;
            arrayLb[5] = lbPid;
            arrayLb[6] = lbPidVar;
            arrayLb[7] = lbTime;
            arrayLb[8] = lbTimeVar;

            plObject.Controls.AddRange(arrayLb);

现在的问题是 - 如何存储这个控件数组以供将来更新(编辑/删除)。 有人可以建议一个解决方法,如何做到这一点?

我的想法是创建一个控件列表

public static List<Control> LiControls = new List<Control>(); 

并存储在其中。但是我要添加很多控件(~200) - 并且很难处理它们。

1 个答案:

答案 0 :(得分:0)

我会创建一个单独的Serializable类,其中包含您需要保留的属性。您可以使用XmlSerializer将其保存到xml文件中,如果以后需要保存更多属性,只需将它们添加到类中,XmlSerializer将正确保存并加载新属性。祝你好运!