Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c# - Saving the form state then opening it back up in the same state

I have a small program in winforms that holds 3 buttons. Thus far the program lets the user change a the color of another button by clicking the corresponding button While the third button does not do anything yet. What I want to do is to let the user save changes made to the form (save the form state). So when the form is reopened it opens in that same state as saved.

I hope I am being clear about what I am after

Here is a visualization of the form:

enter image description here

The code that i have so far if any help:

public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnToColor.Text = "";
        }

        int c = 0;
        private void btnColorSwap_Click(object sender, EventArgs e)
        {
            if (c == 0)
            {
                btnToColor.BackColor = Color.Yellow;
                c++;

            }

            else if (c == 1)
            {
                btnToColor.BackColor = Color.YellowGreen;

                c++;
            }

            else if (c == 2)
            {
                btnToColor.BackColor = Color.LimeGreen;

                c = 0;
            }

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may/may not be easier for you.

Start by creating a class to hold your state:

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

Now, declare a member for your Form with this object:

public partial class Form1 : Form {
    MyFormState state = new MyFormState();

On form load, check if the config exists, then load it:

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

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

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

When your form is closing.. save the config:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    writeConfig();
}

private void writeConfig() {
    using (StreamWriter sw = new StreamWriter("config.xml")) {
        state.ButtonBackColor = System.Drawing.ColorTranslator.ToHtml(button1.BackColor);
        XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
        ser.Serialize(sw, state);
    }
}

Then you can add members to your state class and they will be written into the config.xml file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...