如何让lisbox.SelectedIndex与多个列表框中选择的多个项目一起使用?

时间:2017-07-21 05:22:07

标签: c# listbox listboxitem

我正在创建一系列列表框,我想在第一个项目和第一个列表框中单击它,然后在列表框2中加载项目。然后当我单击列表框2中的项目时,它会加载项目在列表框中3.当我单击列表框1中的项目时,列表框2的项目加载但当我单击列表框2中的项目加载列表框3时没有任何加载,我没有收到错误。不知道为什么这个代码在列表框1工作时不起作用?

        if (listBox1.SelectedIndex == 0 && !listBox2.Items.Contains("Directory50        >"))
        {
            if (listBox2.Items.Count >= 5)
            {
                for (int i = listBox2.Items.Count - 1; i >= 0; i--)
                {
                    // do with listBox1.Items[i]

                    listBox2.Items.RemoveAt(i);
                }
            }
            for (int x = 0; x <= 4; x++)
            {
                listBox2.Items.Add("Directory5" + x.ToString() + "        >");
            }

            if (listBox2.SelectedIndex == 0)
            {///I get no response here from listbox2 when mouse clicking the first item in listbox2
                for (int x = 0; x <= 2; x++)
                {
                    listBox3.Items.Add("Directory51" + x.ToString() + ".txt");
                }

            }

        }

1 个答案:

答案 0 :(得分:0)

listBox1.SelectedIndex > 0 

OR

listBox1.SelectedIndex > -1 (-1 if first item needs to load something)

listBox1.SelectedIndex = -1 // means nothing selected

// means an item selected and first item is with index = 0 
listBox1.SelectedIndex = 0 or 1 or 2 ... etc 
相关问题