列表框中选定的项目 [英] listBox selected item

查看:120
本文介绍了列表框中选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个列表框:

listBox1中有如下内容:水果和蔬菜。

listBox1 have the following items: Fruit and vegetable.

listBox2有。以下项目:橙,苹果,黄瓜和番茄

listBox2 have the following items: Orange, Apple, cucumber and Tomato.

listBox3有以下项目:红,绿,黄,橙

listBox3 have the following items: Red, Green, Yellow and Orange.

和我想做的事情就是这样,如果我在listBox1中选择水果我只想显示Orange和苹果在listBox2,如果我在listBox2选择苹果我想显示红色,绿色和黄色的例子。

And i want to do like this, if i select Fruit in listBox1 i only want to show Orange and Apple in listBox2 and if i select Apple in listBox2 i want to show Red, Green and Yellow for example.

和如果没有在listBox1中选择,然后listBox2和3应是空的,如果没有在listBox2选中然后listBox3应是空的。

And if nothing is selected in listBox1 then listBox2 and 3 shall be empty and if nothing is selected in listBox2 then listBox3 shall be empty.

和是否有做出选择/取消选择方法什么好​​办法?

And is there any good way to make a select/deselect method?

谢谢!

推荐答案

您可以尝试这样的事情,对于易于理解划分功能融入功能。我设计使用取胜的形式,但是你可以在列表框使用相同的代码,以及该代码。

You can try something like this, for easy understanding divide the functionality into functions. I designed this code using win forms however you can apply same code on List boxes as well.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch(comboBox1.SelectedItem.ToString())
        {
            case "Fruit":
                FruitSelected();
                break;
            case "Vegetables":
                VegetableSelected();
                break;
            default:
                NoneSelected();
                break;
        }
    }
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Similar code as above
    }
    protected void FruitSelected()
    {
        comboBox2.Items.Clear();
        comboBox2.Items.Add("Orange");
        comboBox2.Items.Add("Apple");
    }
    protected void VegetableSelected()
    {
        comboBox2.Items.Clear();
        comboBox2.Items.Add("Tomato");
        comboBox2.Items.Add("Cucumber");
    }
    protected void NoneSelected()
    {
        comboBox2.Items.Clear();
        comboBox3.Items.Clear();
    }
}



希望它帮助。

Hope it helps.

这篇关于列表框中选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆