如果没有任何代码源,则更难以指定要做什么,但正如 Nadia 在 cmets 中建议的那样,我也会建议。每当您要触发一个事件来更改应在 CheckedListBox 项目中显示哪些标签时,您应该保存已检查项目的索引列表。您的每个“创建”选项都应该在某个时候保存这些列表之一。然后,当它们被触发重新显示时,将这些标签重置为选中状态。
出于演示目的,以下示例是一个简单的 WinForms 应用程序,其中包含三个用于切换显示的 CheckedListBox 内容的按钮,以及一个用于向当前显示的列表添加其他项目的按钮。
属性
/// <summary>
/// The index of which dictionary Options are being displayed in the CheckedListBox.
/// </summary>
public int SourceIndex { get; set; }
/// <summary>
/// The array which will contain 3 dictionaries filled with data.
/// </summary>
Dictionary<int, string>[] Options { get; set; }
/// <summary>
/// An array of 3 lists (corrosponding to the Options dictionaries)
/// which will contain the indices of checked Options items.
/// </summary>
List<int>[] Checked { get; set; }
构造函数
/// <summary>
/// Initialize the Options and add content to each.
/// New up the Checked lists.
/// </summary>
public Form1()
{
InitializeComponent();
this.SourceIndex = -1;
this.checkedListBox1.CheckOnClick = true;
this.Checked = new List<int>[]
{
new List<int>(),
new List<int>(),
new List<int>(),
};
this.Options = new Dictionary<int,string>[3];
// Fruit Options
this.Options[0] = new Dictionary<int, string>();
this.Options[0].Add(0, "Apple");
this.Options[0].Add(1, "Banana");
this.Options[0].Add(2, "Orange");
this.Options[0].Add(3, "Grapes");
// Meat Options
this.Options[1] = new Dictionary<int, string>();
this.Options[1].Add(0, "Beef");
this.Options[1].Add(1, "Chicken");
this.Options[1].Add(2, "Pork");
this.Options[1].Add(3, "Lamb");
// Drink Options
this.Options[2] = new Dictionary<int, string>();
this.Options[2].Add(0, "Milk");
this.Options[2].Add(1, "Water");
this.Options[2].Add(2, "Juice");
this.Options[2].Add(3, "Soda");
}
活动
/// <summary>
/// Save the displayed option's checked items then load the Fruit options to the CheckedListBox.
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void FruitButton_Click(object sender, EventArgs e)
{
this.SaveCheckedStates();
this.LoadOptions(0);
}
/// <summary>
/// Save the displayed option's checked items then load the Meat options to the CheckedListBox.
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void MeatButton_Click(object sender, EventArgs e)
{
this.SaveCheckedStates();
this.LoadOptions(1);
}
/// <summary>
/// Save the displayed option's checked items then load the Drink options to the CheckedListBox.
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void DrinksButton_Click(object sender, EventArgs e)
{
this.SaveCheckedStates();
this.LoadOptions(2);
}
/// <summary>
/// Add an item to the currently displayed Option's items.
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void AddButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.textBox1.Text) && this.SourceIndex >= 0)
{
int index = this.Options[this.SourceIndex].Count;
string text = this.textBox1.Text;
this.Options[this.SourceIndex].Add(index, text);
this.SaveCheckedStates();
this.LoadOptions(this.SourceIndex);
}
}
私有辅助方法
/// <summary>
/// Load the (indexed) Option's items into the CheckedListBox for display.
/// Reset the Checked state for any previously checked.
/// </summary>
/// <param name="index">The index of which Option's items to load.</param>
private void LoadOptions(int index)
{
this.SourceIndex = index;
this.checkedListBox1.Items.Clear();
foreach (KeyValuePair<int, string> item in this.Options[index])
{
this.checkedListBox1.Items.Add(item);
}
foreach (int i in this.Checked[index])
{
this.checkedListBox1.SetItemChecked(i, true);
}
}
/// <summary>
/// The displayed Options are about to change.
/// Save the currently displayed Option's checked item indices.
/// </summary>
private void SaveCheckedStates()
{
if (this.SourceIndex >= 0)
{
this.Checked[this.SourceIndex].Clear();
foreach (int i in this.checkedListBox1.CheckedIndices)
{
this.Checked[this.SourceIndex].Add(i);
}
}
}
结果
总结
一般的想法是每次显示不同的内容时刷新CheckedListBox.Items。您可以找到一种使用数据绑定的方法,但 CheckedListBox 不会接受 Dictionary 作为来源。照原样,foreach 循环就足够了,而且不会太费力。请注意,将项目添加到您的字典不会影响索引顺序,但如果您计划对 Option 项目进行任何插入/排序/删除,那么您需要在 Checked 索引列表中补偿这些更改。
这只是一个粗略的例子。如果有优化,希望有人会编辑它们。