winform自动保存属性变更

1.每次变更都保存

Properties.Settings.Default.PropertyChanged += Default_PropertyChanged;

private void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    Properties.Settings.Default.Save();
}

2. 退出时保存

Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

void Application_ApplicationExit(object sender, EventArgs e)
{
    Settings.Default.Save();
}

 

转载于:https://www.cnblogs.com/dennysong/p/5579886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用XML序列化将WinForm控件的属性保存到XML文件中。具体步骤如下: 1. 创建一个WinForm应用程序,添加需要保存属性的控件,例如TextBox、CheckBox等。 2. 在项目中创建一个保存控件属性的类。该类需要实现ISerializable接口,以便在XML文件中序列化和反序列化对象。 3. 在类中定义需要保存的控件属性,并添加属性的get和set方法。 4. 在WinForm的窗体代码中,添加保存和读取控件属性的方法。保存方法将控件属性序列化到XML文件中,读取方法从XML文件中反序列化控件属性并设置控件的值。 以下是一个简单的示例代码: ``` using System; using System.IO; using System.Windows.Forms; using System.Xml.Serialization; // 定义一个保存控件属性的类 [Serializable()] public class ControlProperties : ISerializable { public string Name { get; set; } public string Text { get; set; } public bool Checked { get; set; } // 构造函数 public ControlProperties() { } // 反序列化构造函数 public ControlProperties(SerializationInfo info, StreamingContext context) { Name = info.GetString("Name"); Text = info.GetString("Text"); Checked = info.GetBoolean("Checked"); } // 序列化方法 public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", Name); info.AddValue("Text", Text); info.AddValue("Checked", Checked); } } public partial class MainForm : Form { private string propertiesFile = "control_properties.xml"; public MainForm() { InitializeComponent(); } // 保存控件属性到XML文件 private void SaveControlProperties() { ControlProperties[] properties = new ControlProperties[2]; properties[0] = new ControlProperties { Name = textBox1.Name, Text = textBox1.Text }; properties[1] = new ControlProperties { Name = checkBox1.Name, Checked = checkBox1.Checked }; XmlSerializer serializer = new XmlSerializer(typeof(ControlProperties[])); using (TextWriter writer = new StreamWriter(propertiesFile)) { serializer.Serialize(writer, properties); } } // 从XML文件中读取控件属性并设置控件的值 private void LoadControlProperties() { if (File.Exists(propertiesFile)) { XmlSerializer serializer = new XmlSerializer(typeof(ControlProperties[])); using (TextReader reader = new StreamReader(propertiesFile)) { ControlProperties[] properties = (ControlProperties[])serializer.Deserialize(reader); foreach (ControlProperties property in properties) { Control control = Controls.Find(property.Name, true)[0]; if (control is TextBox) ((TextBox)control).Text = property.Text; else if (control is CheckBox) ((CheckBox)control).Checked = property.Checked; } } } } // 窗体加载时读取控件属性 private void MainForm_Load(object sender, EventArgs e) { LoadControlProperties(); } // 窗体关闭时保存控件属性 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { SaveControlProperties(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值