C# VSTO搭配setting功能

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.");
            }
        }
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值