保存表单状态,那么它重新打开相同的状态 [英] Saving the form state then opening it back up in the same state

查看:201
本文介绍了保存表单状态,那么它重新打开相同的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的WinForms 保存3个按钮的小程序。迄今为止,该方案可以让通过点击相应的按钮,而第三个按钮不会做任何事情还没有用户变化的另一个按钮的颜色。我想要做的就是让用户保存到窗体(窗体保存状态)所做的更改。因此,当窗体被重新打开它作为保存的相同的状态打开。



我希望我是清楚我是什么


$之后b $ b

下面是表单的可视化:



在这里输入的形象描述



这是我到目前为止的代码,如果任何帮助:

 公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
btnToColor.Text =;
}

INT C = 0;
私人无效btnColorSwap_Click(对象发件人,EventArgs五)
{
如果(C == 0)
{
btnToColor.BackColor = Color.Yellow;
C ++;

}

,否则如果(C == 1)
{
btnToColor.BackColor = Color.YellowGreen;

C ++;
}

,否则如果(C == 2)
{
btnToColor.BackColor = Color.LimeGreen;

C = 0;
}

}
}


解决。方案

这可能/可能不会对你更容易



开始通过创建一个类来保存你的状态:

 公共类MyFormState {
公共字符串ButtonBackColor {搞定;组; }
}

现在,声明成员为你的与此对象:

 公共部分Form1类:表格{
MyFormState状态=新MyFormState ();

在窗体加载,检查配置存在,然后加载它:

 私人无效Form1_Load的(对象发件人,EventArgs五){
如果(File.Exists(config.xml中)){
loadConfig();
}

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

私人无效loadConfig(){
XmlSerializer的SER =新的XmlSerializer(typeof运算(MyFormState));
使用(的FileStream FS = File.OpenRead(config.xml中)){
=状态(MyFormState)ser.Deserialize(FS);
}
}

当您的窗体关闭..保存的配置:

 私人无效Form1_FormClosing(对象发件人,FormClosingEventArgs E){
writeConfig();
}

私人无效writeConfig(){使用(StreamWriter的SW =新的StreamWriter(config.xml中))
{
state.ButtonBackColor = System.Drawing中.ColorTranslator.ToHtml(button1.BackColor);
XmlSerializer的SER =新的XmlSerializer(typeof运算(MyFormState));
ser.Serialize(SW,状态);
}
}



然后就可以将成员添加到您的状态类,它们将被写入config.xml文件。


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:

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;
            }

        }
    }

解决方案

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.

这篇关于保存表单状态,那么它重新打开相同的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆