Winform如何保存自定义属性

1.在设计界面中选择控件,然后在属性窗口选择数据->ApplicationSettings,然后选择font,新建一个fontsize

2.添加控制类

    public class fontcontrol
    {
        //AppConfig ac = new AppConfig();
        public float Size
        {
            get
            {
                return PE_TEST.Properties.Settings.Default.fontsize.Size;
            }
            set
            {
                Font f = PE_TEST.Properties.Settings.Default.fontsize;
                Font nf = new Font(f.FontFamily, value, f.Style, f.Unit);
                PE_TEST.Properties.Settings.Default.fontsize = nf;
                //Properties.Settings.Default.Save();
            }
        }
    }

3.在窗口类的事件响应中调用

        private void Btnadd_Click(object sender, EventArgs e)
        {
            FontCtrl.Size++;
        }


4.点击项目属性中的“设置”中的查看代码,vs自动生成Settings.cs文件。在Settings.cs中的初始化中绑定事件
        public Settings() {
            // // 若要为保存和更改设置添加事件处理程序,请取消注释下列行:
            //
            // this.SettingChanging += this.SettingChangingEventHandler;
            //
            // this.SettingsSaving += this.SettingsSavingEventHandler;
            //
            this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Settings_PropertyChanged);
        }

5.在Settings.cs中的PropertyChanged事件响应函数中

        void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Settings s = sender as Settings;
            s.Save();
        }

posted on 2013-04-18 17:02  qiu2013  阅读(1037)  评论(1编辑  收藏  举报

导航