Question

I have a small program that generates four dynamic buttons on load in a flowLayoutPanel1 and a static save button.

My aim is to save the dynamic button colors so that when the form is reloaded or opened again the colors of the dynamic buttons are loaded back up in the same state as saved.

Below I will include my code which has been commented to demonstrate what I have attempted. I am using an xml file to save the button state and included a class that holds the state. However the method I am using works fine if a create my buttons to save statically. (I have only tried it with one static button)

Interface: enter image description here class that holds state:

  public class MyFormState
    {
        public string ButtonBackColor { get; set; }
    }

Form code:

 public partial class Form1 : Form
    {
        //Form Member
        MyFormState state = new MyFormState();
        Button btn;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //loading xml file if it exists
            if (File.Exists("config.xml"))
            {
                loadConfig();

            }

            //Genrating dynamic buttons
            //  flowLayoutPanel1.Controls.Clear();
            for (int i = 0; i <= 3; ++i)
            {
                btn = new Button();
                btn.Text = " Equation " + i;

                flowLayoutPanel1.Controls.Add(btn);
                //click event of buttons
                btn.Click += new EventHandler(btn_Click);
            }

           btn.BackColor = System.Drawing.ColorTranslator.FromHtml(state.ButtonBackColor);


        }

        //method to load file
        private void loadConfig()
        {

            XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
            using (FileStream fs = File.OpenRead("config.xml"))
            {
                state = (MyFormState)ser.Deserialize(fs);
            }
        }

        //saving the xml file and the button colors
        private void writeConfig()
        {
            using (StreamWriter sw = new StreamWriter("config.xml"))
            {

                state.ButtonBackColor = System.Drawing.ColorTranslator.ToHtml(btn.BackColor);
                XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
                ser.Serialize(sw, state);
            }
        }
        int count = 0;


        //backcolor change
        void btn_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;

            if (count == 0)
            {
                button.BackColor = Color.Red;
                count++;
            }

            else if (count == 1)
            {
                button.BackColor = Color.Blue;
                count--;
            }
        }

        //save file
        private void btnSave_Click(object sender, EventArgs e)
        {
            writeConfig();
        }



    }

Any suggestions on what i should change so that it works for me? Thanks

Était-ce utile?

La solution

Your code works fine but only for the last button. You have 4 buttons but only have one MyFormState object. When creating the buttons you always use the same private field btn. So you need arrays of 4 MyFormState objects and 4 buttons.

MyFormState[] states = new MyFormState[4];
Button[] buttons = new Button[4];

private void Form1_Load(object sender, EventArgs e)
{
    if (File.Exists("config.xml"))
    {
        loadConfig();
    }

    for (int i = 0; i < 4; ++i)
    {
        buttons[i] = new Button();
        buttons[i].Text = " Equation " + i;

        flowLayoutPanel1.Controls.Add(buttons[i]);
        buttons[i].Click += new EventHandler(btn_Click);

        if (states[i] != null)
        {
            buttons[i].BackColor = ColorTranslator.FromHtml(states[i].ButtonBackColor);
        }
    }
}

private void loadConfig()
{
    XmlSerializer ser = new XmlSerializer(typeof(MyFormState[]));
    using (FileStream fs = File.OpenRead("config.xml"))
    {
        states = (MyFormState[])ser.Deserialize(fs);
    }
}

private void writeConfig()
{
    for (int i = 0; i < 4; i++)
    {
        if (states[i] == null)
        {
            states[i] = new MyFormState();
        }
        states[i].ButtonBackColor = ColorTranslator.ToHtml(buttons[i].BackColor);
    }

    using (StreamWriter sw = new StreamWriter("config.xml"))
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyFormState[]));
        ser.Serialize(sw, states);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top