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

您所在的位置:网站首页 vb列表框添加选项 Combobox下拉列表框的常用属性与事件

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

2023-08-12 01:54| 来源: 网络整理| 查看: 265

下拉列表框 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 dic = new Dictionary(); 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; //设置选中项 } } }

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3