Combobox下拉列表框的常用属性与事件

本文介绍了Windows Form中的ComboBox控件的使用方法,包括如何通过不同方式添加数据、设置默认选项及如何实现控件间的联动效果。文章还展示了如何利用C#进行编程实现。

下拉列表框 ComboBox
快速监视快捷键:Shift+F9(VS2017)

text 属性获取的数据为控件显示的内容.
SelectedItem 是获取当前选择的对象.
如果当前DropDownStyle 为DropDown时,可以更改下拉框中的内容.那么将会引发使用SelectedItem的null异常.
为了避免用户修改,我们一般会设置如果当前DropDownStyle为如果当前DropDownList

如果需要做一个联动的逻辑.
首先获取 cmb1 选择的数据
然后设置 cmb2 选择的项目.


/*string[] strs= new string[3];
 * strs[0]="窗体";
 * strs[1]="修炼";
 * 与下面等价
 * 
 *string[] strs= new string[] { "窗体", "修炼", "手册" };  初始化器
 *cmb下拉框.Items.AddRange(strs);
 * 与下面是等价
 *
 *cmb下拉框.Items.AddRange(new string[] { "窗体", "修炼", "手册" });
*/

打开字符串集合编辑器
在这里插入图片描述
或者在这里插入图片描述
在这里插入图片描述
运行结果:
在这里插入图片描述

打开快速监视:
添加断点——>运行调试——>选中项——>右键选择QuickWatch或者快捷键Shift+F9
在这里插入图片描述
在这里插入图片描述
界面如下:
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Dictionary<string, string> dic = new Dictionary<string, string>();

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = comboBox1.Items.Count - 1; //选中最后一项

            //代码添加数据方法一:
            comboBox2.Items.Add("阿加莎·克里斯蒂");
            comboBox2.Items.Add("查尔斯·狄更斯");
            comboBox2.Items.Add("阿瑟·柯南道尔");
            comboBox2.Items.Add("加西亚·马尔克斯");
            comboBox2.Items.Add("夏洛蒂·勃朗特");
            comboBox2.Items.Add("简·奥斯汀");
            comboBox2.SelectedIndex = 3;

            //代码添加数据方法二:
            comboBox3.Items.AddRange(new string[] { "杨门女将", "岳飞", "东郭先生", "八仙过海" }); //初始化器
            comboBox3.SelectedIndex = 2;

            string[] strs = new string[] { "哪吒闹海", "醉八仙", "渔岛怒潮", "胆剑篇", "孙悟空外传" };
            comboBox4.Items.AddRange(strs);  //35+36两行代码的作用<==>33行代码
            comboBox4.SelectedIndex = 0;

            dic.Add("北京985大学", "清华,北大,人大,北师大,北航,北理,中国农大,中央民族");
            dic.Add("上海985大学", "复旦,同济,上交大,华东师范");
            dic.Add("湖南985大学", "湖南,中南,国防科大");
            dic.Add("陕西985大学", "西交大,西北工业,西北农林科技");
            dic.Add("广东985大学", "华南理工,中山");
            dic.Add("江苏985大学", "南大,东南");
            dic.Add("辽宁985大学", "大连理工,东北大学");
            dic.Add("山东985大学", "山大,中国海洋");
            dic.Add("四川985大学", "四川,电子科技");
            dic.Add("天津985大学", "南开,天大");
            dic.Add("湖北985大学", "武大,华中科技");

            foreach(var item in dic.Keys)
            {
                comboBox6.Items.Add(item.ToString());
            }
            comboBox6.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string textstr = comboBox1.Text; //属性获取的数据为控件显示的内容
            object obj = comboBox1.SelectedItem; //获取当前选择的对象
            string selecttext = comboBox1.SelectedItem.ToString();
            string str = comboBox1.SelectedText;
            object sval = comboBox1.SelectedValue;
            MessageBox.Show(selecttext);
        }

        //数据联动1
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox5.SelectedIndex = comboBox2.SelectedIndex;
        }

        //数据联动2
        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox7.Items.Clear();  //清空原有数据
            string selectext = comboBox6.SelectedItem.ToString(); //获取选中项
            string strvalue = dic[selectext];  //使用选中项目作为key获取dic中的value
            string[] strs = strvalue.Split(',');  //将字符串数据转换为数组
            comboBox7.Items.AddRange(strs);  //添加到comboBox7中
            comboBox7.SelectedIndex = 0;     //设置选中项
        }
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值