【问题标题】:Writing form state to xml C#将表单状态写入xml C#
【发布时间】:2017-04-09 07:20:02
【问题描述】:

我的目标是通过按钮单击保存所有表单数据(而不是在关闭时)。为此,我使用了以下线程中给出的示例。 Saving the form state then opening it back up in the same state

我已尝试尽我所能调整我的代码,但没有任何反应,也没有显示任何错误。我做错了什么?

这是我的代码的相关部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WindowsFormsApplication1
{
    public partial class frmPayroll : Form
    {
        SaveData sd = new SaveData();
        public frmPayroll()
        {
            InitializeComponent();
        }
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            writeConfig();
        }
        private void writeConfig()
        {
            using (StreamWriter sw = new StreamWriter("config.xml"))
            {
                sd.Married = rdoMarr.Checked;
                sd.PayPd = cbPayPd.Text;
                sd.Allow = cbAllow.Text;
                sd.Gross = txtGross.Text;
                sd.Fit = txtFit.Text;
                sd.Soc = txtSoc.Text;
                sd.Med = txtMed.Text;
                sd.NetPay = txtNet.Text;
                sd.PayPd = cbPayPd.Text;
                XmlSerializer ser = new XmlSerializer(typeof(SaveData));
                ser.Serialize(sw, sd);
            }
        }
        private void frmPayroll_Load(object sender, EventArgs e)
        {
            if (File.Exists("config.xml"))
            {
                loadConfig();
            }
            sd.Married = rdoMarr.Checked;
            sd.PayPd = cbPayPd.Text;
            sd.Allow = cbAllow.Text;
            sd.Gross = txtGross.Text;
            sd.Fit = txtFit.Text;
            sd.Soc = txtSoc.Text;
            sd.Med = txtMed.Text;
            sd.NetPay = txtNet.Text;
        }
        private void loadConfig()
        {
            XmlSerializer ser = new XmlSerializer(typeof(SaveData));
            using (FileStream fs = File.OpenRead("config.xml"))
            {
                sd = (SaveData)ser.Deserialize(fs);
            }
        }
    }
    public struct SaveData
    {
        public bool Married;
        public string PayPd;
        public string Allow;
        public string Gross;
        public string Fit;
        public string Soc;
        public string Med;
        public string NetPay;
    }
}

【问题讨论】:

    标签: c# xml visual-studio-2015 xml-serialization xml-deserialization


    【解决方案1】:

    您正在通过反序列化加载您的对象。

    但是您在哪里将状态分配回您的控件?

    查看frmPayroll_Load 函数。

    您正在尝试再次将数据分配回对象。

    您必须将数据分配回表单控件。

    应该是这样的(如果需要,您可能需要应用数据转换):

    rdoMarr.Checked = sd.Married;
    .
    .
    .
    .
    txtFit.Text = sd.Fit;
    .
    .
    .
    .
    

    【讨论】:

    • 谢谢!!我一尝试就奏效了。我绞尽脑汁想弄清楚我做错了什么。我正在学习 c# 课程,但没有学习任何先决条件,因此有时很难理解 MSDN 上的许多教程或示例在说什么。现在尝试添加一个清除按钮。
    • 我试图对你的答案进行投票,但我还没有足够的代表。
    • 很抱歉,感谢您的帮助。我是新来的,但现在已经完成了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多