- Tạo bản quyền phần mềm C# và bảo mật code | The Enigma Protector
- [C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform
- [C#] Lập trình ứng dụng lấy ngày giờ hệ thống mạng LAN sử dụng giao thức UDP
- [DATABASE] Sự khác nhau giữa hai câu lệnh TRUNCATE vs DELETE trong sqlserver
- [C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
[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
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:
Ở 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!