NEWS

[C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform

[C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
Đăng bởi: Thảo Meo - Lượt xem: 2819 08:43:59, 05/12/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách sử dụng control Property Grid để lưu và tải lại cấu hình của Winform C#.

[C#] How to using Property Grid Save and Load user settings

Dưới đây là hình ảnh giao diện demo ứng dụng:

save-form-setting_property_grid

Ở hình trên các bạn thấy mình lưu trữ lại các thông tin như: Caption, Font, BackColor, ForeColor...

Khi người dùng chỉnh sửa thông tin và lưu lại cấu hình, lần sau khi mở Form sẽ load lại đúng thông tin cấu hình của người đó.

Đầu tiên, các bạn tạo một class WinformSettings.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;
using System.Drawing;
using System.Windows.Forms;

namespace SaveSetttingWithPropertyGrid
{
    [DefaultPropertyAttribute("Caption")]
    internal class WinformSettings
    {
        private string _caption;
        private Font _font;
        private FormWindowState _formWindowState;
        private FormBorderStyle _borderStyle;
        private Color _backColor;
        private Color _foreColor;

        [CategoryAttribute("Giao diện"), DescriptionAttribute("Kiều màu viền")]
        public FormBorderStyle BorderStyle
        {
            get
            {
                return _borderStyle;
            }
            set
            {
                _borderStyle = value;
            }
        }

        [CategoryAttribute("Giao diện"), DescriptionAttribute("Màu nền form")]
        public Color BackColor
        {
            get
            {
                return _backColor;
            }
            set
            {
                _backColor = value;
            }
        }
        [CategoryAttribute("Giao diện"), DescriptionAttribute("Màu chữ form")]
        public Color ForeColor
        {
            get
            {
                return _foreColor;
            }
            set
            {
                _foreColor = value;
            }
        }

        [CategoryAttribute("Cài đặt chung"), DescriptionAttribute("Tiêu đề của Form")]
        public string Caption
        {
            get
            {
                return _caption;
            }
            set
            {
                _caption = value;
            }
        }
       
       
        [CategoryAttribute("Cài đặt chung"), DescriptionAttribute("Font chữ")]
        public Font Font
        {
            get
            {
                return _font;
            }
            set
            {
                _font = value;
            }
        }

        [CategoryAttribute("Cài đặt chung"), DescriptionAttribute("Trạng thái form khi khởi động")]
        public FormWindowState FormWindowState
        {
            get
            {
                return _formWindowState;
            }
            set
            {
                _formWindowState = value;
            }
        }

        public WinformSettings() { }
    }
}

Các bạn có thể chỉnh sửa hoặc thêm thông tin tùy ý.

Tiếp đến, mình sẽ sử dụng thư viện Newtonsoft để lưu trữ thông tin cấu hình đó xuống dạng file Json.

Source code Form1.cs:

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

namespace SaveSetttingWithPropertyGrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            propertyGrid1.PropertyValueChanged += PropertyGrid1_PropertyValueChanged;
        }

        private void PropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            var formSetting = propertyGrid1.SelectedObject as WinformSettings;
            this.Text = formSetting.Caption;
            this.WindowState = formSetting.FormWindowState;
            this.BackColor = formSetting.BackColor;
            this.ForeColor = formSetting.ForeColor;
            this.FormBorderStyle = formSetting.BorderStyle;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.userSettings))
            {
                var userSettingsJson = Properties.Settings.Default.userSettings;
                var userSettings = JsonConvert.DeserializeObject<WinformSettings>(userSettingsJson);
                this.Text = userSettings.Caption;
                this.WindowState = userSettings.FormWindowState;
                this.BackColor = userSettings.BackColor;
                this.ForeColor = userSettings.ForeColor;
                this.FormBorderStyle = userSettings.BorderStyle;
                propertyGrid1.SelectedObject = userSettings;
            }
            else {
                propertyGrid1.SelectedObject = new WinformSettings();
            }

           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var formSetting = propertyGrid1.SelectedObject as WinformSettings;
            var json = JsonConvert.SerializeObject(formSetting);
            Properties.Settings.Default.userSettings = json;
            Properties.Settings.Default.Save();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
Đăng bởi: Thảo Meo - Lượt xem: 2819 08:43:59, 05/12/2022DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.