ComboBox 类详解
ComboBox 类
表示 Windows 组合框控件。
有关此类型所有成员的列表,请参阅 ComboBox 成员。
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ComboBox
[Visual Basic]
Public Class ComboBox
Inherits ListControl
[C#]
public class ComboBox : ListControl
[C++]
public __gc class ComboBox : public ListControl
[JScript]
public class ComboBox extends ListControl
线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。
备注
ComboBox 显示与一个 ListBox 组合的编辑字段,使用户可以从列表中选择或输入新文本。ComboBox 的默认行为是显示一个编辑字段,该字段附带一个隐藏的下拉列表。DropDownStyle 属性确定要显示的组合框的样式。您可以输入一个值,该值指示允许以下情况:简单的下拉列表(始终显示列表)、下拉列表框(文本部分不可编辑,并且必须选择一个箭头才能查看下拉列表框)或默认下拉列表框(文本部分可编辑,并且用户必须按箭头键才能查看列表)。若要始终显示用户不能编辑的列表,请使用 ListBox 控件。
若要在运行时向列表添加对象,请用 AddRange 方法分配一个对象引用数组。然后,列表显示每个对象的默认字符串值。可以用 Add 方法添加单个对象。
除显示和选择功能外,ComboBox 还提供一些功能使您能够有效地向 ComboBox 添加项以及在列表项内查找文本。BeginUpdate 和 EndUpdate 方法使您能够向 ComboBox 添加大量项而不必在每次向列表添加项时重新绘制控件。FindString 和 FindStringExact 方法使您能够在列表中搜索包含特定搜索字符串的项。
可以使用这些属性管理列表中当前选定的项,使用 Text 属性指定编辑字段中显示的字符串,使用 SelectedIndex 属性获取或设置当前项,以及使用 SelectedItem 属性获取或设置对对象的引用。
示例
[Visual Basic, C#, C++] 下面的示例是个完整的应用程序,阐释可如何使用 Add 方法向 ComboBox 添加项,如何使用 FindString 方法在 ComboBox 中查找项,以及如何使用 BeginUpdate 和 EndUpdate 方法以高效的方式向 ComboBox 添加大量项。
[C#]
using System;
using System.Windows.Forms;
namespace Win32Form1Namespace {
public class Win32Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button addGrandButton;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button showSelectedButton;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button findButton;
private System.Windows.Forms.Label label1;
public Win32Form1() {
this.InitializeComponent();
}
[System.STAThreadAttribute()]
public static void Main() {
System.Windows.Forms.Application.Run(new Win32Form1());
}
private void InitializeComponent() {
this.addButton = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.addGrandButton = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.showSelectedButton = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.findButton = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.addButton.Location = new System.Drawing.Point(248, 32);
this.addButton.Size = new System.Drawing.Size(40, 24);
this.addButton.TabIndex = 1;
this.addButton.Text = "Add";
this.addButton.Click += new System.EventHandler(this.addButton_Click);
this.textBox2.Location = new System.Drawing.Point(8, 64);
this.textBox2.Size = new System.Drawing.Size(232, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.addGrandButton.Location = new System.Drawing.Point(8, 96);
this.addGrandButton.Size = new System.Drawing.Size(280, 23);
this.addGrandButton.TabIndex = 2;
this.addGrandButton.Text = "Add 1,000 Items";
this.addGrandButton.Click += new System.EventHandler(this.addGrandButton_Click);
this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.comboBox1.DropDownWidth = 280;
this.comboBox1.Items.AddRange(new object[] {"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"});
this.comboBox1.Location = new System.Drawing.Point(8, 248);
this.comboBox1.Size = new System.Drawing.Size(280, 21);
this.comboBox1.TabIndex = 7;
this.showSelectedButton.Location = new System.Drawing.Point(8, 128);
this.showSelectedButton.Size = new System.Drawing.Size(280, 24);
this.showSelectedButton.TabIndex = 4;
this.showSelectedButton.Text = "What Item is Selected?";
this.showSelectedButton.Click += new System.EventHandler(this.showSelectedButton_Click);
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Size = new System.Drawing.Size(232, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.findButton.Location = new System.Drawing.Point(248, 64);
this.findButton.Size = new System.Drawing.Size(40, 24);
this.findButton.TabIndex = 3;
this.findButton.Text = "Find";
this.findButton.Click += new System.EventHandler(this.findButton_Click);
this.label1.Location = new System.Drawing.Point(8, 224);
this.label1.Size = new System.Drawing.Size(144, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Test ComboBox";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.comboBox1,
this.textBox2,
this.textBox1,
this.showSelectedButton,
this.findButton,
this.addGrandButton,
this.addButton,
this.label1});
this.Text = "ComboBox Sample";
}
private void addButton_Click(object sender, System.EventArgs e) {
comboBox1.Items.Add(textBox1.Text);
}
private void addGrandButton_Click(object sender, System.EventArgs e) {
comboBox1.BeginUpdate();
for (int i = 0; i < 1000; i++) {
comboBox1.Items.Add("Item 1" + i.ToString());
}
comboBox1.EndUpdate();
}
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}
private void showSelectedButton_Click(object sender, System.EventArgs e) {
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "/n" +
"Index: " + selectedIndex.ToString());
}
}
}
ComboBox 类详解相关推荐
- OpenCV Mat类详解和用法(官网原文)
参考文章:OpenCV Mat类详解和用法 我马克一下,日后更 官网原文链接:https://docs.opencv.org/3.2.0/d6/d6d/tutorial_mat_the_basic_i ...
- 转载:c+string类详解
C++ string 类详解 </h1><div class="clear"></div><div class="postBod ...
- JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- JDBC学习笔记01【JDBC快速入门、JDBC各个类详解、JDBC之CRUD练习】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- Android复习14【高级编程:推荐网址、抠图片上的某一角下来、Bitmap引起的OOM问题、三个绘图工具类详解、画线条、Canvas API详解(平移、旋转、缩放、倾斜)、矩阵详解】
目 录 推荐网址 抠图片上的某一角下来 8.2.2 Bitmap引起的OOM问题 8.3.1 三个绘图工具类详解 画线条 8.3.16 Canvas API详解(Part 1) 1.transla ...
- Java中的Runtime类详解
Java中的Runtime类详解 1.类注释 /**Every Java application has a single instance of class Runtime that allows ...
- [NewLife.XCode]实体类详解
NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含 ...
- basicdatasourcefactory mysql_Java基础-DBCP连接池(BasicDataSource类)详解
Java基础-DBCP连接池(BasicDataSource类)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际开发中"获得连接"或"释放资源 ...
- JAVA的StringBuffer类详解
JAVA的StringBuffer类详解 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer ...
最新文章
- vector容器中erase(删除)的使用
- 使用poi进行数据的导出Demo
- 【PP模块】报废(损耗)类别简介(Scrap Categories and Their Effects)
- HDLBits答案(12)_Verilog移位寄存器
- 栈溢出利用-----jmp esp
- 单缸发动机扭矩动力学计算:理论计算virtual.lab motion仿真
- MATLAB学习笔记(十一)
- c语言实验转换字母顺序结构,实验1顺序结构的程序设计-实验报告.doc
- 单片机p2.0引脚c语言,单片机p2.0?
- oracle停数据库服务器,优化Oracle停机时间及数据库恢复
- /usr/include/pcap/pcap.h源码
- mysql的英文字母_MYSQL中查询怎么判断一个字段包含英文?
- 2014清华计算机系直博名单,2014年清华大学博士研究生拟录取名单公示
- XP系统 mscorsvw.exe进程 占CPU资源 开机加载网络连接很慢 解决方法
- not a valid identifier
- 一眼万年:AI眼底筛查为什么能够成为AI医疗中的“落地之王”
- 重启服务器上的MYSQL
- 高斯列主元消去法解线性方程组
- 程序员的时间管理计划
- HDU6848改编题(弱化)——客星璀璨之夜(stars)
热门文章
- elementUI表格中动态单元格合并
- Mybatis基于注解实现增删查改和多参数列表查询
- Description	Resource	Path	Location	Type Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER w
- SAFNet 基于相似性感知的三维语义分割融合网络
- Zerotier——免费的虚拟局域网 | 内网穿透 | 解决方案
- echarts实现大数据拖拽数据图表
- 推荐一个搜题小程序,一起搜题小程序,搜题找答案不用注册也没次数限制
- 斗鱼直播Android开发二面被刷,手慢无
- 图解css3:核心技术与案例实战. 2.11 属性选择器
- 英语专业有必要学python吗-马哥教育官网-专业Linux培训班,Python培训机构