- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[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!