ComBox、listBox、checklistBox控件

omBox控件被称为下拉组合框控件,是由System.windows.Forms.ComBox类提供的,主要作用是讲一个集合数据以组合框的形式显示给用户,当用户单击时将以下拉框显示给用户,供用户选择一项。

ListBox控件是由System.Windows.forms.ListBox提供的,主要作用是件给一个集合数据以列表框的形式显示给用户从中选择一项或多项

 

属性

selectionMode         one 只能选择一个 None 选择不了 Multisimple 多项选择

CheckedListBox控件是由System.windows.froms.CheckedListBox类提供的,比较适合用于代替多个checkBox列,如果选择一个人的爱好时,就要用到许多选择,如果用checkBox控件,就要用多个checkbox,如果用CheckedListBox控件只要用一个即可

 

三个控件的共性

 

这三个控件都有一个统一的存放集合的属性,Items属性,可通过selectedltems返回选择对象.ListBox控件和checkedListBox控件都有一个selectionMode属性,用于设置是单项选择还是多项选择或者是不选择。
对于三个控件,当用户更改选择项时,都可响应selectedIndexChanged事件

 private void Form1_Load(object sender, EventArgs e)
        {
            //将label567控件的text设置为空,这样在主窗体就没有显示了
            label5.Text = "";
            label6.Text = "";
            label7.Text = "";
            label8.Text = "";
            //首先我们给集合Items赋值,是多个值 所以用到数组和循环
            string [] strItems = { "语文","数学","英语","美术","体育","C#"};
            for (int i = 0; i < strItems.Length; i++)
            {
                //为ComBox赋值
                //combox1中的集合附加上字符串数组中的i
                comboBox1.Items.Add(strItems[i]);
                listBox1.Items.Add(strItems[i]);
                checkedListBox1.Items.Add(strItems[i]);
            }

        }
        //为ComBox创建一个selectedIndexchanged事件(当属性值更改的时候发生事件)
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //label5.text=ComBox.选择的集合中找
           label5.Text = (string)comboBox1.SelectedItem;
        }
        //为ListBox创建事业selectedIndexChanged事件
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label6.Text = (string)listBox1.SelectedItem;
        }
        //为checkedListBox创建事件selectedIndexChanged
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label7.Text = (string)checkedListBox1.SelectedItem;
            //给label8重新赋值,要不每次选中都会显示
            label8.Text = "";
            //这句的意思是,循环遍历输出结果
            foreach (string outstr in checkedListBox1.CheckedItems)
            {
                //label8中的txet要+=与oustr “ ”是加个空格的意思
                label8.Text += outstr+" ";
            }
            
        }
    }
原文地址:https://www.cnblogs.com/xiaowie/p/8608955.html