VSTO & setting
常常USER在使用選單時如果有多個選擇要選,在下一次打開介面時又要重新選擇,造成USER使用上的不便利,所以VSTO有沒有辦法解決這個問題呢?
答案是有的,要搭配C#內建功能setting來使用
setting在方案總管,屬性之中
看到後點兩下,裡面是我已經預設寫好的內容,否則打開應該會是空白的
要製作的結果就是選擇左邊的項目,勾選起來後,在combobox裡面輸入儲存名稱,接著按下按鈕1儲存這個名稱以及勾選結果,在下一次時可以選擇這個名稱且快速勾選要的項目
按鈕2則是刪除紀錄,代碼直接貼在下面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Specialized;
namespace WPS_COMPARE
{
public partial class Form1 : Form
{
private Dictionary<string, List<string>> comboBoxItems = new Dictionary<string, List<string>>();
public Form1()
{
InitializeComponent();
LoadData();
button2.Click += button2_Click;
}
private void button1_Click(object sender, EventArgs e)
{
string comboBoxName = comboBox1.Text;
if (!string.IsNullOrEmpty(comboBoxName))
{
List<string> selectedItems = new List<string>();
foreach (var item in checkedListBox1.CheckedItems)
{
selectedItems.Add(item.ToString());
}
if (comboBoxItems.ContainsKey(comboBoxName))
{
comboBoxItems[comboBoxName] = selectedItems;
}
else
{
comboBoxItems.Add(comboBoxName, selectedItems);
comboBox1.Items.Add(comboBoxName);
}
SaveData();
MessageBox.Show("Saved successfully!");
}
else
{
MessageBox.Show("Please enter a name.");
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedName = comboBox1.SelectedItem.ToString();
if (comboBoxItems.ContainsKey(selectedName))
{
List<string> selectedItems = comboBoxItems[selectedName];
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
foreach (var item in selectedItems)
{
int index = checkedListBox1.Items.IndexOf(item);
if (index >= 0)
{
checkedListBox1.SetItemChecked(index, true);
}
}
}
}
private void SaveData()
{
Properties.Settings.Default.ComboBoxItems = new StringCollection();
Properties.Settings.Default.CheckedListBoxItems = new StringCollection();
foreach (var key in comboBoxItems.Keys)
{
Properties.Settings.Default.ComboBoxItems.Add(key);
var selectedItems = string.Join(",", comboBoxItems[key]);
Properties.Settings.Default.CheckedListBoxItems.Add($"{key}:{selectedItems}");
}
Properties.Settings.Default.Save();
}
private void LoadData()
{
if (Properties.Settings.Default.ComboBoxItems != null)
{
comboBox1.Items.Clear();
comboBoxItems.Clear();
foreach (var key in Properties.Settings.Default.ComboBoxItems)
{
comboBox1.Items.Add(key);
}
foreach (var item in Properties.Settings.Default.CheckedListBoxItems)
{
var parts = item.Split(':');
if (parts.Length == 2)
{
var key = parts[0];
var selectedItems = parts[1].Split(',').ToList();
comboBoxItems[key] = selectedItems;
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string selectedName = comboBox1.SelectedItem?.ToString();
if (!string.IsNullOrEmpty(selectedName) && comboBoxItems.ContainsKey(selectedName))
{
comboBoxItems.Remove(selectedName);
comboBox1.Items.Remove(selectedName);
comboBox1.Text = string.Empty;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
SaveData();
MessageBox.Show("Deleted successfully!");
}
else
{
MessageBox.Show("Please select a name to delete.");
}
}
}
}