在我的程序中,我在开始时有八个按钮(每个按钮代表房子里的灯)。用户可以向程序添加新按钮(灯)。我在FlowLayoutPanel(FLP)中有这些,每次程序关闭时都会保存表单的当前状态,包括FLP的高度和宽度位置以及按钮的当前信息(包括它们的名称,文本,颜色等)到XML文件。
如果FLP的位置或大小发生变化,当程序重新加载时,它们会像你想象的那样更新,如果按钮有变化,那么它们将被更新。但是,我提供的defualt八个按钮,如果用户添加一个新按钮或几个,然后它们被保存到xml文件但程序重新加载,从该xml读取,这些新按钮被丢弃。
对此有任何想法。
当前代码:读取XML文件(这是来自另一个.cs文件)
if (roomCtrl is Button)
{
xmlSerialisedForm.WriteElementString("Text", ((Button)roomCtrl).Text);
xmlSerialisedForm.WriteElementString("Backcolor",((Button)roomCtrl).BackColor.ToString());
}
if (roomCtrl is FlowLayoutPanel)
{
xmlSerialisedForm.WriteElementString("Width", ((FlowLayoutPanel)roomCtrl).Size.Width.ToString());
xmlSerialisedForm.WriteElementString("Height", ((FlowLayoutPanel)roomCtrl).Size.Height.ToString());
xmlSerialisedForm.WriteElementString("X", ((FlowLayoutPanel)roomCtrl).Location.X.ToString());
xmlSerialisedForm.WriteElementString("Y",((FlowLayoutPanel)roomCtrl).Location.Y.ToString());
}
当前代码:从XML文件读取(这是来自另一个.cs文件)
case "System.Windows.Forms.Button":
if (n["Backcolor"].InnerText == "Color [LawnGreen]")
{
((System.Windows.Forms.Button)ctrlToSet).BackColor = System.Drawing.Color.LawnGreen;
}
else if (n["Backcolor"].InnerText == "Color [Tomato]")
{
((System.Windows.Forms.Button)ctrlToSet).BackColor = System.Drawing.Color.Tomato;
}
break;
case "System.Windows.Forms.FlowLayoutPanel":
((System.Windows.Forms.FlowLayoutPanel)ctrlToSet).Size = new System.Drawing.Size(Convert.ToInt32(n["Width"].InnerText), Convert.ToInt32(n["Height"].InnerText));
((System.Windows.Forms.FlowLayoutPanel)ctrlToSet).Location = new System.Drawing.Point(Convert.ToInt32(n["X"].InnerText), Convert.ToInt32(n["Y"].InnerText));
if (controlType == "System.Windows.Forms.Button")
{
Button b = new Button();
b.Name = controlName;
b.Text = n["Text"].InnerText;
if (n["Backcolor"].InnerText == "Color [LawnGreen]")
{
((System.Windows.Forms.Button)ctrlToSet).BackColor = System.Drawing.Color.LawnGreen;
{
((System.Windows.Forms.Button)ctrlToSet).BackColor = System.Drawing.Color.Tomato;
}
FlowLayoutPanel flpSockets = (FlowLayoutPanel)ctrlToSet;
flpSockets.Controls.Add(b);
}
break;
我认为我在浏览FLP的xml文件时遗漏了一些内容,但不确定。
添加按钮的代码(这是另一种形式)
private void button2_Click(object sender, EventArgs e)
{
if (rt.roomBool == true)
{
socket = new Button();
socket.Name = "btn"+txtSocketName.Text;
socket.Text = txtSocketName.Text;
socket.Size = new System.Drawing.Size(70, 60);
socket.BackColor = Color.LawnGreen;
rt.flpSockets.Controls.AddRange(new System.Windows.Forms.Control[] { this.socket });
rt.flpSockets.Height = 199;
rt.flpSockets.Location = new System.Drawing.Point((rt.flpSockets.Location.X), 20);
rt.Show();
}
从xml文件目的地读取的代码
FormSerialisor.Serialise(this, Application.StartupPath + @"\roomTemplate.xml");