我试图动态地更改CheckedListBox中的项源,并保留它们所选的值?
CheckedListBox1 | CheckedListBox2
[x] list0 | [ ] list0item0
[ ] list1 | [ ] list0item1
[ ] list2 | [ ] list0item2
[ ] list3 | [ ] list0item3当选择list1 (未选中,只是高级别)时,更新CheckedListBox2项
CheckedListBox1 | CheckedListBox2
[ ] list0 | [ ] list1item0
[x] list1 | [ ] list1item1
[ ] list2 | [ ] list1item2
[ ] list3 | [ ] list1item3这是一个描述我问题的图片。
下面是一个代码片段:
public void customModuleFunctionsCheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//checks before calling this function if there is any element selected..
for (int i = 0; i < this.mainForm.customFunctionList[index].Items.Count; i++)
{
if (this.mainForm.customFunctionList[index].SelectedIndex == i)
{
this.mainForm.customFunctionUseCasesList[index].Items.Clear();
//this.mainForm.customFunctionUseCasesList[index].ItemsSourceOrWhateverMethodIs = aListOfStrings....
}
}
}有什么干净的解决办法吗?提前感谢!
发布于 2015-11-13 13:12:56
我自己问题的答案可能是这个。但是我需要在CheckedListBox1中放置字符串而不是(集合)。
public partial class Form1 : Form
{
List<List<object>> function;
List<object> useCase;
public Form1()
{
function = new List<List<object>>();
useCase =new List<object>();
InitializeComponent();
useCase.AddRange(new object[] {
"List0Item0",
"List0Item1",
"List0Item2",
"List0Item3",
"List0Item4",
"List0Item5"});
this.function.Add(this.useCase);
this.checkedListBox1.Items.Add(function);
useCase = new List<object>();
useCase.AddRange(new object[] {
"List1Item0",
"List1Item1",
"List1Item2",
"List1Item3",
"List1Item4",
"List1Item5"});
this.function.Add(this.useCase);
this.checkedListBox1.Items.Add(function);
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.checkedListBox2.Items.Clear();
for (int i = 0; i < this.function.Count(); i++)
{
if (checkedListBox1.SelectedIndex == i)
{
for (int j = 0; j < this.function[i].Count(); j++)
{
this.checkedListBox2.Items.Add(this.function[i][j]);
}
}
}
}
}附加这里的代码的输出。
发布于 2015-11-13 10:18:06
我不是c#的专家,更像是新手!但我是这样想的。当某人从listBox1(单击)中选择一个值时,它会清除默认情况下或以前的查询中所执行的listBox2结果和查询。在选择listBox1项之后,listBox2 onClick将根据选择的listBox1项执行查询。
if(listBox1.Selecteditem == "Fruit")
{
query to database WHERE item is Fruit
}不过,我无法确认,但我发现了一些关于这种列表框所选内容的文档。
希望这会有帮助!)
https://stackoverflow.com/questions/33689912
复制相似问题