WinForm——ComboBox总结

网友投稿 982 2022-05-29

常用属性

DropDownStyle属性

ComboBox 的DropDownStyle 属性 一共有三个值:DropDownList, DropDown, Simple。

字段

说明

DropDown

通过单击下箭头指定显示列表,并指定文本部分可编辑。 这表示用户可以输入新的值,而不仅限于选择列表中现有的值。 使用此设置时,Append 的 AutoCompleteMode 值的作用与 SuggestAppend 值相同。 这是默认样式。

DropDownList

通过单击下箭头指定显示列表,并指定文本部分不可编辑。 这表示用户不能输入新的值。 只能选择列表中已有的值。 只有在 AutoCompleteMode 为 Suggest 或 SuggestAppend 时才会显示列表。

Simple

指定列表始终可见,并指定文本部分可编辑。 这表示用户可以输入新的值,而不仅限于选择列表中现有的值。

SelectedIndex属性

表示获取或设置指定当前选定项的索引,如果未选定任何项则返回值为-1

常用方法

Select方法

该方法从ComboBox选取指定的项。语法格式public void Select(int start,int length);

start:表示文本框中当前选定文本的第一位字符的位置。。

length:表示要选择的字符数。

SelectAll方法

该方法用来选择ComboBox控件可编辑部分的所有文本。

常用事件

DropDown事件

当显示ComboBox的下拉部分时发生。

DropDownClosed事件

在ComboBox的下拉部分不可见时发生。

SelectedIndexChanged事件

在SelectedIndex属性更改后发生。

SelectedValueChanged事件

在SelectedValue属性改变时发生。

实用举例

验证SelectedIndexChanged、DropDown、DropDownClosed事件

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.richTextBox1.ReadOnly = true;

string[] arr = { "高尔夫球", "乒乓球", "羽毛球", "排球" };

for (int i = 0; i < arr.Length; i++)

{

this.comboBox1.Items.Add(arr[i]);

}

this.comboBox1.SelectedIndex = 0;

}

private void comboBox1_DropDown(object sender, EventArgs e)

{

this.richTextBox1.BackColor = Color.BurlyWood;

this.richTextBox1.ForeColor = Color.BlueViolet;

this.richTextBox1.Text = "开启";

this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

}

private void comboBox1_DropDownClosed(object sender, EventArgs e)

{

this.richTextBox1.BackColor = Color.BurlyWood;

this.richTextBox1.ForeColor = Color.BlueViolet;

this.richTextBox1.Text = "关闭";

this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

MessageBox.Show("选项的索引:" + this.comboBox1.SelectedItem.ToString());

}

}

绑定DataSet数据

//绑定DataSet数据源

private void BindcmbArea()

{

if (bllArea==null)//实例化对象

{

bllArea=BLL.MetaData.HotelArea.Instance();

}

DataSet ds=bllArea.GetDataSet();//用DataSet获取数据源

cmb.DataSource=ds.Table[0].defaultView;//指定数据源

cmb.DisplayMember=”AreaName”;//绑定Text的值,获取值用cmb.Text

cmb.ValueMember=”AreaID”;//绑定Value值

}

绑定数据源后,默认添加“全部”项。

if (bllArea==null)//实例化对象

{

bllArea=BLL.MetaData.HotelArea.Instance();

}

DataSet ds=bllArea.GetDataSet();//用DataSet获取数据源

DataTable table=new DataTable();

DataColumn dc=new DataCoumn(“AreaID”,typeof(int));

table.Columns.Add(dc);

dc=new DataColumn(“AreaName”,typeof(string));

table.Columns.Add(dc);

DataRow row=table.NewRow();

row[0]=0;

row[1]=”全部”;

for(int i=0;i

{

row=table.NewRow();

row[0]=ds.Tables[0].Rows[i][“AreaID”];

row[1]= ds.Tables[0].Rows[i][“AreaName”];

table.Rows.Add(row);

}

cmb.DataSource=table;//指定数据源

cmb.DisplayMember=”AreaName”;//绑定Text的值,获取值用cmb.Text

cmb.ValueMember=”AreaID”;//绑定Value值

注:当两个ComboBox指定同一个对象的时候,一个ComboBox的值发生变化的时候,另一个ComboBox也改变的解决方法

cmb2.DataSurce=table.Copy();

cmb2.DisplayMember=”AreaName”;//绑定Text的值,获取值用cmb.Text

WinForm——ComboBox总结

cmb2.ValueMember=”AreaID”;//绑定Value值

cmb2.SelectedIndex=0;

绑定List数据源增加“请选择”项

private void BindType()

{

List typeList=_TypeBLL.GetList();

if (typeList>0)

{

typeList.Insert(0,new TicketTypeInfo(){TypeID=0,TypeCode=”0”,TypeName=”请选择”});

cmb.DataSource=typeList;

cmb.DisplayMember=”TypeName”;

cmb.ValueMember=”TypeCode”;

}

}

美化ComboBox,让ComboBox每个Item前面加上一个图片

将数据库中图片读取到imageList1中,用来保存图片。

设置ComboBox的属性。将DrawMode设置为OwnerDrawFixed,DropDownStyle设置为DropDownList

//绑定数据

private void cmbBind()

{

List dicValueList=_dicValueBLL.GetList();

foreach(DicValueInfo dic in dicValueList)

{

imageList1.Images.Add(dic.DicItemID.ToString(),AddHeader(dicValueInfo.Icon16));// AddHeader方法将byte数组转为图片。

cmb.Items.Add(dic.ShowName.ToString());

}

if (cmb.Items.Cout>0)

{

cmb.SelectedIndex=0;

}

}

//绑定事件DrawItem

private void cmbDrawItem(object sender,DrawItemEventArgs e)

{

Graphics gCom=e.Graphics;//声明一个GDT+绘图图面类的对象。

Rectangle rCom=e.Bounds;//声明一个表示举行的位置和大小类的对象。

Size imageSize=imageList1.ImageSize;//声明一个有序证书对的对象。

FontDialog typeface=new FontDialog();//定义一个字体类的对象。

Font style=typeface.Font;//定义一个特定的文本格式类对象

if (e.Index>=0)//当绘制的索引项存在时。

{

string temp=(string)cmb.Items[e.Index].ToString();//获取ComboBox控件索引项的文本内容。

StringFormat stringFormat=new StringFormat();//定义一个封装文本布局信息类的对象。

stringFormat.Alignment=StringAlignment.Near;//设定文本的布局方式。

if(e.state==(DrawItemState.NoAccelerator|DrawItemState.NoFocusRect))//当绘制项没有键盘加速键和焦点可视化提示时

{

e.Graphics.FillRectangle(new SolidBrush(Color.Red),rCom);//用指定的颜色填充自定义矩形的内部。

imageList1.Draw(e.Graphics,rCom.Left,rCom.Top,e.Index);//在指定位置绘制指定索引的图片

e.Graphics.DrawString(temp,new SolidBrush(Color.Black),rCom.Left+imageSize.Width,rCom.Top);//在指定的位置,指定的Font对象绘制指定的文本字符串。

e.DrawFocusRectangle();//在指定的边界范围内绘制聚集框

}

else//当绘制项有键盘加速键或者焦点可是化提示时

{

e.Graphics.FillRectangle(new SolidBrush(Color.LightBule),rCom);//用指定的颜色填充自定义矩形难过的内部。

imageList1.Draw(e.Graphics,rCom.Left,rCom.Top,e.Index);//指定位置索引的图片

e.Graphics.DrawString(temp,style,new SolidBrush(Color.Black),rCom.Left+imageSize.Width,rCom.Top);//在指定位置并且用指定的Font对象绘制指定的文本字符串。

e.DrawFocusRectangle();//在指定的边界范围内绘制聚焦框。

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:黑白棋人机对战完整代码
下一篇:Cookie&amp;Session笔记
相关文章