首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变更CheckedListBox ItemsSource

变更CheckedListBox ItemsSource
EN

Stack Overflow用户
提问于 2015-11-13 09:52:44
回答 2查看 419关注 0票数 2

我试图动态地更改CheckedListBox中的项源,并保留它们所选的值?

代码语言:javascript
复制
 CheckedListBox1 |   CheckedListBox2
[x] list0        |   [ ] list0item0
[ ] list1        |   [ ] list0item1
[ ] list2        |   [ ] list0item2
[ ] list3        |   [ ] list0item3

当选择list1 (未选中,只是高级别)时,更新CheckedListBox2项

代码语言:javascript
复制
 CheckedListBox1 |   CheckedListBox2
[ ] list0        |   [ ] list1item0
[x] list1        |   [ ] list1item1
[ ] list2        |   [ ] list1item2
[ ] list3        |   [ ] list1item3

这是一个描述我问题的图片

下面是一个代码片段:

代码语言:javascript
复制
    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....

                    }
                }
            }

有什么干净的解决办法吗?提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-13 13:12:56

我自己问题的答案可能是这个。但是我需要在CheckedListBox1中放置字符串而不是(集合)。

代码语言:javascript
复制
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]);
                    }
                }
            }
        }
    }

附加这里的代码的输出。

票数 0
EN

Stack Overflow用户

发布于 2015-11-13 10:18:06

我不是c#的专家,更像是新手!但我是这样想的。当某人从listBox1(单击)中选择一个值时,它会清除默认情况下或以前的查询中所执行的listBox2结果和查询。在选择listBox1项之后,listBox2 onClick将根据选择的listBox1项执行查询。

代码语言:javascript
复制
if(listBox1.Selecteditem == "Fruit")
{
  query to database WHERE item is Fruit
}

不过,我无法确认,但我发现了一些关于这种列表框所选内容的文档。

Microsoft ListBox选定项文档

希望这会有帮助!)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33689912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档