- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time 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
[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!