在我的應用程式中,我有一些 checkListBoxes,我需要保存對這個串列的點擊。這意味著下次我不需要一次又一次地點擊它們。
代碼:
public Form2()
{
InitializeComponent();
InitializeSecondForm();
}
private void InitializeSecondForm()
{
this.Height = Properties.Settings.Default.SecondFormHeight;
this.Width = Properties.Settings.Default.SecondFormWidth;
this.Location = Properties.Settings.Default.SecondFormLocation;
this.FormClosing = SecondFormClosingEventHandler;
this.StartPosition = FormStartPosition.Manual;
}
private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SecondFormHeight = this.Height;
Properties.Settings.Default.SecondFormWidth = this.Width;
Properties.Settings.Default.SecondFormLocation = this.Location;
Properties.Settings.Default.Save();
}
我試圖用這個問題來回答我的問題,但它不起作用:Save CheckedListBox Items to Settings
使用簡單的復選框,這不是問題,但這里有一個串列。
知道怎么做嗎?
uj5u.com熱心網友回復:
也許將每個檢查專案的識別符號保存到一個 json 檔案中。
在下面我在 CheckedListBox 上做的,對于多個 CheckedListBox 控制元件,您需要調整代碼以使用一個具有修改結構的 json 檔案來說明多個 CheckedListBox 控制元件或每個 CheckedListBox 一個 json 檔案。
示例,將專案加載到以下類中
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public override string ToString()
{
return ProductName;
}
}
使用以下類讀取/寫入 json 檔案,在這種情況下使用json.net但也適用于system.text.json.
public class JsonOperations
{
/// <summary>
/// In your app you need to setup a different file name for each CheckedListBox
/// </summary>
public static string FileName =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Checked.json");
/// <summary>
/// Save only checked products
/// </summary>
/// <param name="list"></param>
public static void Save(List<ProductItem> list)
{
string json = JsonConvert.SerializeObject(list, Formatting.Indented);
File.WriteAllText(FileName, json);
}
/// <summary>
/// Read back if file exists
/// </summary>
/// <returns></returns>
public static List<ProductItem> Read()
{
List<ProductItem> list = new List<ProductItem>();
if (File.Exists(FileName))
{
list = JsonConvert.DeserializeObject<List<ProductItem>>(File.ReadAllText(FileName));
}
return list;
}
}
以下類提供了從上面提到的 json 中獲取已選中項和已選中項的方法。
public static class CheckedListBoxExtensions
{
public static List<ProductItem> IndexList(this CheckedListBox sender)
{
return
(
from item in sender.Items.Cast<Product>()
.Select(
(data, index) =>
new ProductItem()
{
ProductID = data.ProductID,
Index = index
}
)
.Where((x) => sender.GetItemChecked(x.Index))
select item
).ToList();
}
public static void SetChecked(this CheckedListBox sender, int identifier, bool checkedState = true)
{
var result = sender.Items.Cast<Product>()
.Select((item, index) => new CheckItem
{
Product = item,
Index = index
})
.FirstOrDefault(@this => @this.Product.ProductID == identifier);
if (result != null)
{
sender.SetItemChecked(result.Index, checkedState);
}
}
}
公共類 CheckItem { 公共產品產品 { 獲取;放; }公共整數索引{獲取;放; } }
表單代碼將類似于以下內容以讀取表單顯示事件上的選中項并在表單關閉事件上保存選中項。
public partial class SaveItemsForm : Form
{
private List<Product> _products = new List<Product>();
public SaveItemsForm()
{
InitializeComponent();
Shown = OnShown;
Closing = OnClosing;
}
private void OnClosing(object sender, CancelEventArgs e)
{
List<ProductItem> checkedItems = ProductCheckedListBox.IndexList();
if (checkedItems.Count > 0)
{
JsonOperations.Save(checkedItems);
}
else
{
JsonOperations.Save(new List<ProductItem>());
}
}
private void OnShown(object sender, EventArgs e)
{
_products = SqlServerOperations.ProductsByCategoryIdentifier(1);
ProductCheckedListBox.DataSource = _products;
/*
* Search for each product by id, if in the CheckedListBox check it
*/
var items = JsonOperations.Read();
if (items.Count >0 )
{
items.ForEach( x => ProductCheckedListBox.SetChecked(x.ProductID));
}
}
}
uj5u.com熱心網友回復:
在這種情況下,我更喜歡像 SQLite 這樣的簡單資料庫。使用此資料庫保存用戶組態檔。用戶個人資料資料將存盤所有此類資料。例如,可以將用戶名和密碼保存在里面,以便用戶下次登錄時記住,或者用戶在使用系統時選擇的所有設定和配置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332900.html
