In my program I have eight buttons to begin with at the start (each of these representing a light in the house). The user is able to add a new button (light) to the program. I have these in a FlowLayoutPanel(FLP) and every time the program closes it saves the current state of the form, including position of the FLP its Height and Width and the current information of the buttons (including their names,text, colours etc) to an XML file.
If the FLP has it's position or size change, when the program re-loads they will be updated, like you would think and if the buttons have had something changed then they shall be updated. However, Bar the defualt eight buttons i provided, if the user adds a new button or a few, then they get saved into the xml file but the the program reloads, reading from that xml, those new buttons are discarded.
Any thoughts on this.
Current code: Reading to XML file (this is from another .cs file)
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());
}
Current code: Reading from XML file (this is from another .cs file)
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;
I think I'm missing something in the reading from xml file for FLP's but not sure.
Code for adding button (this is from another form)
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();
}
Code for reading from the xml files destination
FormSerialisor.Serialise(this, Application.StartupPath + @"\roomTemplate.xml");