- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
[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!