Hi there,

In my program i have given the user the possibillity to add new buttons to the form I then serialize the form to a xml file so it is saved. Any data that is edited while the program is running will be saved (for example user edits the buttons text) for next time the program runs, and it does successfully.

However when they add a new button, it get serialized to the xml file but when i run the program again and read from the xml file, that new button does not exist.

Code for serializing the forms data to the xml, saving any info about the buttons.

if (roomCtrl is Button)
                    {
                        xmlSerialisedForm.WriteElementString("Text", ((Button)roomCtrl).Text);
                        xmlSerialisedForm.WriteElementString("Backcolor", ((Button)roomCtrl).BackColor.ToString());
                        xmlSerialisedForm.WriteElementString("X", ((Button)roomCtrl).Location.X.ToString());
                        xmlSerialisedForm.WriteElementString("Y", ((Button)roomCtrl).Location.Y.ToString());
                    }

code for deserialing from the xml.

case "System.Windows.Forms.Button":


                                     ((System.Windows.Forms.Button)ctrlToSet).Name = controlName;
                                     ((System.Windows.Forms.Button)ctrlToSet).Text = n["Text"].InnerText;
                                    ((System.Windows.Forms.Button)ctrlToSet).Location = new System.Drawing.Point(Convert.ToInt32(n["X"].InnerText), Convert.ToInt32(n["Y"].InnerText));


                                    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;

Any ideas on what it might be or if i am just missing something??

Thanks in advanced.

LNC

Recommended Answers

All 2 Replies

You are not instantiating a new button into memory. Try using

Button newBtn = new Button();
//Set attributes from XML here
myForm.Controls.Add(newBtn);

Skatamatic, ive updated the code and it looks like below. However this doesn't do anything either. I've madde the user be able to edit the buttons text but that now doesn't save.

Any suggestions?

Form newFrm = new Form();
Button newBtn =new Button();
newBtn.Name = controlName;
newBtn.Text = n["Text"].InnerText;
newBtn.Location = new System.Drawing.Point(Convert.ToInt32(n["X"].InnerText), Convert.ToInt32(n["Y"].InnerText));

if (n["Backcolor"].InnerText == "Color [LawnGreen]")
{
newBtn.BackColor = System.Drawing.Color.LawnGreen;
}
else if (n["Backcolor"].InnerText == "Color [Tomato]")
{
newBtn.BackColor = System.Drawing.Color.Tomato;
}
newFrm.Controls.Add(newBtn);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.