ComboBox是文本框和列表框的组合,使用更为灵活也更常用
1.下拉样式
DropDownStyle属性是最重要的一个属性,用于指定控件的样式。
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); SetComboBoxDropDownStyle(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } }
常见的样式有
Simple 用户只能输入文本,不能进行下拉列表选择
private void SetComboBoxDropDownStyle() { //设置下拉样式为单纯文本框 this.comboBox1.DropDownStyle = ComboBoxStyle.Simple; }
DropDownList 用户只能使用下拉列表进行选择
private void SetComboBoxDropDownStyle() { //设置下拉样式为只能列表选择 this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; }
DropDown 用户就可以文本输入也可以列表选择,综合了上面两种情况
private void SetComboBoxDropDownStyle() { //设置下拉样式为文本和列表结合 this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown; }
2.重要属性
2.1 DroppedDown 指示在程序运行中是否显示下拉列表
private void SetComboBoxDropDownStyle() { //设置下拉样式 this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;//这里要显示指定 this.comboBox1.DroppedDown = true; }
2.2 DropDownHeight 指示下拉列表的高度,当对下拉列表高度大于各项的高度总和时候进行trim
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); SetDroppedDownHeight(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } private void SetDroppedDownHeight() { this.comboBox1.DropDownHeight = 200;//设置下拉高度 } }
2.3 DropDownWidth 指示控件宽度,默认控件宽度和下拉宽度相等
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); SetDroppedDownWidth(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } private void SetDroppedDownWidth() { this.comboBox1.DropDownWidth = 200;//设置下拉高度 } }
2.4 MaxDropDwnItems 指示下拉列表的可显示的最大项数量.当2.2和2.4冲突的时候,以2.4属性为准
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); SetMaxDropDownItems(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } private void SetMaxDropDownItems() { this.comboBox1.MaxDropDownItems = 2;//最大显示2项 } }
3.自动补齐(也适合TextBox)
AutoCompleteCustomSource 进行匹配的数据源之一,和Items类似
AutoCompleteSource 是一个枚举值表示用什么数据源进行补全
AutoCompleteMode表示补全时候数据出现的方式
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); SetAutoCompleteCustomSource(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } private void SetAutoCompleteCustomSource() { this.comboBox1.AutoCompleteCustomSource. AddRange(new string[]{"CustomItem1","CustomItem2", "CustomItem3","CustomItem4","CustomItem5", "CustomItem6"}); this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems; //按照下拉列表进行补全 this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;//只是建议不自动补全 }
对于二枚举值有FileSystem,将文件系统作为数据源补全
FileSystemDirectories,将文件系统中目录作为数据源
RecentlyUsedList 近期访问的URL补全
HistoryList,AllUrl,AllSystemSources
CustomSource则将上面第一个属性作为数据源
对于三,还有SuggestAppend自动补全且提示,Append自动补全
4.事件
4.1 DropDown 当展开下拉列表的时候触发
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); } private void addItems() { this.comboBox1.Items.Add("Item1"); this.comboBox1.Items.Add("Item2"); this.comboBox1.Items.Add("Item3"); } private void comboBox1_DropDown(object sender, EventArgs e) { MessageBox.Show("DropDown事件被触发"); } private void comboBox1_DropDownClosed(object sender, EventArgs e) { MessageBox.Show("DropDownClosed事件被触发"); } }
在Designer.cs中InitializeComponent方法中加上
this.comboBox1.DropDown+=new System.EventHandler(this.comboBox1_DropDown); this.comboBox1.DropDownClosed+=new System.EventHandler(this.comboBox1_DropDownClosed);
展开列表的时候
关闭列表的时候
4.2 SelectedIndexChanged事件,当选中某一项的时候触发
private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(14, 50); this.comboBox1.MaxDropDownItems = 5; this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(100, 20); this.comboBox1.TabIndex = 0; this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(15, 14); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(97, 21); this.textBox1.TabIndex = 1; // // ComboBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.textBox1); this.Controls.Add(this.comboBox1); this.Name = "ComboBox"; this.Text = "ComboBox"; this.ResumeLayout(false); this.PerformLayout(); }
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); } private void addItems() { for (int i = 0; i < 100;i++ ) { for (int j = 0; j < i; j++) { this.comboBox1.Items.Add("item" + j.ToString()); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { System.Windows.Forms.ComboBox comboBox = (System.Windows.Forms.ComboBox)sender; string selectedItem = (string)comboBox.SelectedItem; int count = 0; int resultIndex = -1; resultIndex = comboBox.FindStringExact(selectedItem); //和listbox一样的方法 while (resultIndex!=-1) { comboBox.Items.RemoveAt(resultIndex);//删除 count += 1; resultIndex = comboBox.FindStringExact(selectedItem, resultIndex); } this.textBox1.Text = selectedItem + " 共有" + count + "个"; } }选中item7之后的结果
4.3 DrawItem事件,当进行绘制项的时候触发.
4.4 MeasureItem事件,当描述DrawItem控件中的可选项的时候触发,比如高度变化
public partial class ComboBox : Form { public ComboBox() { InitializeComponent(); addItems(); } private string[] colors; private void addItems() { colors = new string[] { "红", "黄", "蓝" }; for (int i = 0; i < colors.Length;i++ ) { this.comboBox1.Items.Add(colors[i]); } } private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { float size = 0; System.Drawing.Font font; FontFamily family = null; System.Drawing.Color color = new System.Drawing.Color(); switch (e.Index) //正在描绘第几项 { case 0: size = 35; color = System.Drawing.Color.Red; family = FontFamily.GenericMonospace; break; case 1: size = 25; color = System.Drawing.Color.Yellow; family = FontFamily.GenericMonospace; break; case 2: size = 15; color = System.Drawing.Color.Blue; family = FontFamily.GenericMonospace; break; } e.DrawBackground();//绘制背景 Rectangle rectangle = new Rectangle(0, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);//这里e.Bounds.Height由MeasureItem事件提供 e.Graphics.FillRectangle(new SolidBrush(color), rectangle);//绘制左边矩形 //绘制下拉框字体 font = new Font(family, size, FontStyle.Regular);//正常的字体样式 e.Graphics.DrawString(colors[e.Index], font, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height));//绘制文字,这里width由MeasureItem提供 e.DrawFocusRectangle();//绘制各子项* } private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e) { switch (e.Index) { case 0: e.ItemHeight = 40; break; case 1: e.ItemHeight = 30; break; case 2: e.ItemHeight = 20; break; } e.ItemWidth=100; } }
private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.comboBox1.DropDownHeight = 300; this.comboBox1.FormattingEnabled = true; this.comboBox1.IntegralHeight = false; this.comboBox1.Location = new System.Drawing.Point(10, 20); this.comboBox1.MaxDropDownItems = 5;//最多展示5个 this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(140, 22); this.comboBox1.TabIndex = 0; this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);//注册两个事件 this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem); // // ComboBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.comboBox1); this.Name = "ComboBox"; this.Text = "ComboBox"; this.ResumeLayout(false); }