0

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");
Defterniko
  • 183
  • 2
  • 4
  • 13
  • 1
    I don't see where you are creating a new button, or when you are adding it to the FlowLayoutPanel's control collection. – LarsTech Jul 20 '12 at 14:45
  • Have you compared the saved xml with the original one to see that they are being output? Secondly, double check that you are reading the xml from the saved location? – saj Jul 20 '12 at 14:46
  • I have compared the saved to the original and the saved always has the new content in for 'button9'. and ive added the code for button add function – Defterniko Jul 20 '12 at 14:53
  • You need that same code in your loading xml routine. – LarsTech Jul 20 '12 at 14:57
  • @LarsTech which bit of code do you mean? – Defterniko Jul 20 '12 at 15:03
  • `Button b = new Button();`, plus your properties, then `flpSockets.Controls.Add(b);` or something like that should be somewhere in that code where you are reading the xml. Your xml data should be telling you that a new button needs to be created and added to the FlowLayoutPanel, but the code you posted doesn't demonstrate that. – LarsTech Jul 20 '12 at 15:10
  • @LarsTech i have added the code for a new button and some properties as you suggested to the reading xml part (if you look above at the new code) however i am still getting the same results with nothing appearing. – Defterniko Jul 20 '12 at 17:00
  • Pretty hard to answer without seeing more of the file and more of the code because it isn't clear where `controlType` is coming from and why the FlowLayoutPanel case is the one adding the button instead of the Button case. – LarsTech Jul 20 '12 at 17:08

0 Answers0