combobox.selectedindex

合集下载

combox取值的用法c#-个人经验

combox取值的用法c#-个人经验

comboxIlist 如果绑定的是List那需要绑定实体类里的属性名comboBox1.DisplayMember User_NamecomboBox1.ValueMember User_Ide 注意手动赋值comboBox1.SelectedIndex -1的时候也会触发comboBox1_SelectedIndexChanged事件需要得到被选中的文本时用的是comboBox1.Text不是comboBox1.SelectedText //SelectedText是指被鼠标框中的文本不是选中项的文本//SelectedItem是指选中项所指向的对象如果//IList a collection.GetUser //comboBox1.DataSource a //那么SelectedItem就代表IList里的其中一项//如果toString的话就会出现“namespace.class”(这是一个名称的字符串)所以如果要获取combox中的value和值当当使用SelectedValueChanged方法时应该采用如下方法:product_catecombParentCate.SelectedItem.Cate_id 强转的类型改成你对应的实体类如果在点击某个按钮事件的时候触发,需要采用如下方法combParentCate.SelectedValue.ToString private void frmSelectProduct_Loadobject sender EventArgs e combParentCate.DataSourcedbProductCate.getAllCateDatasetForCombox combParentCate.DisplayMember cate_name combParentCate.ValueMember cate_id//combParentCate1.SelectedIndex -1 //初始化产品列表string sql select a.product_ida.cate_id a.product_name sql a.item_pricea.product_stockb.cate_name sql from product_list aproduct_cateb where a.cate_idb.cate_iddgvSelectStockList.ColumnHeadersDefaultCellStyle.Alignment DataGridViewContentAlignment.MiddleCenter dgvSelectStockList.DataSourcedbProductList.getAllProductBySqlsql private void combParentCate_SelectedValueChangedobject sender EventArgs e //combParentCate1.GetItemTextcate_id.ToString //int tmpCateIdConvert.ToInt32combParentCate1.SelectedValue.ToString string sql select a.product_ida.cate_id a.product_name sqla.item_price a.product_stockb.cate_name sql from product_list aproduct_cate b where a.cate_idb.cate_id if tmpCateId 0 sql and a.cate_id tmpCateIddgvSelectStockList.ColumnHeadersDefaultCellStyle.Alignment DataGridViewContentAlignment.MiddleCenter dgvSelectStockList.DataSourcedbProductList.getAllProductBySqlsql private voidbtnSearch_Clickobject sender EventArgs e int tmpCateId Convert.ToInt32combParentCate.SelectedValue.ToString stringsql select a.product_ida.cate_id a.product_name sql a.item_pricea.product_stockb.cate_name sql from product_list aproduct_cateb where a.cate_idb.cate_id if tmpCateId0 sql and a.cate_id tmpCateIddgvSelectStockList.ColumnHeadersDefaultCellStyle.Alignment DataGridViewContentAlignment.MiddleCenter dgvSelectStockList.DataSourcedbProductList.getAllProductBySqlsql datasetcombox 如:DataSet userDs dbSysAdminUser.getAllUserDataset combUser.DataSource userDs.Tablessys_adminuser combUser.DisplayMember user_name combUser.ValueMember user_id /// 用户名更换的时候提示权限private void combUser_SelectedValueChangedobject sender EventArgs e DataRowView tmpdrv DataRowViewcombUser.SelectedItem int tmpUserId Convert.ToInt32tmpdrv.Rowuser_id.ToString permissions_group tmpGroupIddbSysAdminUser.getSysUserByIdConvert.ToInt32tmpUserId.G roup_id lbPermission.Text tmpGroupId.Group_name。

C#读取xml中的配置信息,并加入到combobox或者其他中(winform)

C#读取xml中的配置信息,并加入到combobox或者其他中(winform)

C#读取xml中的配置信息,并加⼊到combobox或者其他中(winform)<?xml version="1.0" encoding="utf-8" ?><webSiteList><site><siteName>天涯社区</siteName><itemlist></itemlist><itemlist>综合的论坛讨论社区</itemlist></site><site><siteName>猫扑⽹</siteName><itemlist></itemlist><itemlist>国内最⼤的分栏式论坛,综合社区</itemlist><itemlist>没啥好说的了!</itemlist></site></websitelist>设combobox⾥的是猫扑⽹ 天涯社区所在的siteNameprivate void comboBox1_SelectedIndexChanged(object sender, EventArgs e){XmlDocument doc = new XmlDocument();doc.Load("c:\\aa.xml");XmlNode node = doc.SelectSingleNode("webSiteList/site[siteName='" + boBox1.Text + "']");for (int i = 0; i < node.ChildNodes.Count; i++){Console.WriteLine(node.ChildNodes[i].InnerText);}}XmlDocument doc = new XmlDocument();doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication2\XMLFile3.xml");XmlNode node = doc.SelectSingleNode("/webSiteList/site[siteName='[color=#FF0000]天涯社区'[/color]]");//combox.Textforeach (XmlNode el in node.ChildNodes){Debug.WriteLine("nodeType:" + el.NodeType);Debug.WriteLine("Text:" + el.InnerText);}以下内容为程序代码:1 - <items num="6">2 <item>album</item>3 <item>calender</item>4 <item>card</item>5 <item>notebook</item>6 <item>personulbook</item>7 <item>poster</item>8 </items>以上为xml代码以下为C#代码XmlDocument doc = new XmlDocument();doc.Load(Application.StartupPath + "\\xmldocument.xml"); //读取的xml⽂件路径XmlNodeList node = doc.GetElementsByTagName("item");for (int i = 0; i < node.Count; i++){comboBox.Items.Add(node[i].InnerText);}comboBox.SelectedIndex = 0;msdn资料XmlDocument 成员XmlNode 成员XPath 表达式的上下⽂。

Combox控件SelectChange事件

Combox控件SelectChange事件

SelectionChangeCommitted事件在设定datasource和displaymember和valuemember时,以及手动改变combobox索引和值时都不触发该事件, 只是在界面选择combobox不同值时触发。

SelectedIndexChanged事件当 SelectedIndex 属性更改时发生,索引改变时触发。

SelectedValueChanged事件当 SelectedValue 属性更改时发生,值改变时触发。

--------------------------------------------------------------------------------------------------------------------------区别:1、SelectedIndexChanged和SelectedValueChanged可以通过编程的方式更改属性而触发事件,但SelectionChangeCommitted事件必须由用户操作选定选项才能触发。

2、在初始化时(设置源)SelectedIndexChanged和SelectedValueChanged都会被调用,而SelectionChangeCommitted没有这个问题。

3、SelectionChangeCommitted也有一个小问题,当打开下拉菜单,用键盘的上下光标键选择条目后(不用鼠标点击),鼠标点击其他控件,使焦点转移,此时comboBox的Text 属性已改变,SelectedIndex属性也已改变,但这样的操作不会触发SelectionChangeCommitted事件。

结论:SelectionChangeCommitted一定要由鼠标选择才能触发(个人感觉)。

--------------------------------------------------------------------------------------------------------------------------关于事件的触发设置DataSource, DisplayMember, ValueMember不同顺序触发SelectedIndexChanged事件和SelectedValueChanged事件的次数顺序一:boBox1.DisplayMember = "userName"; (不触发)boBox1.ValueMember = "userAge"; (只触发SelectedValueChanged)boBox1.DataSource = dt; (都触发)SelectedValueChanged触发两次(总是先触发)SelectedIndexChanged,触发一次(设置DisplayMember属性时不触发两种事件)顺序二:boBox1.ValueMember = "userAge";boBox1.DisplayMember = "userName";boBox1.DataSource = dt;同上顺序三:boBox1.DataSource = dt; (都触发)boBox1.DisplayMember = "userName";(都触发)boBox1.ValueMember = "userAge";(只触发SelectedValueChanged事件)SelectedValueChanged触发三次(总是先触发)SelectedIndexChanged,触发两次(设置DisplayMember属性时不触发两种事件)顺序四:boBox1.DataSource = dt; (都触发)boBox1.ValueMember = "userAge";(都触发) 其中SelectedValueChanged触发两次boBox1.DisplayMember = "userName";(都触发)SelectedValueChanged触发四次(总是先触发)SelectedIndexChanged,触发三次(设置DisplayMember属性时不触发两种事件)说明:当设置了DataSource属性后,SelectedValue属性值会默认为第一行(所以会触发SelectedIndexChanged事件和SelectedValueChanged事件),因此,如果不希望ComboBox 自动选择第一行,还需在设置完DataSource 后自行将SelectedValue 设为“”。

一个给文件批量重命名的程序

一个给文件批量重命名的程序

一个给文件批量重命名的程序编程背景前两天下载了一些歌曲的MV文件,放在车载U盘上使用,发现车载大屏不支持分类播放,这样,我在U盘上分类存放的几类视频都按“文件名”的先后顺序罗列在播放列表里。

想听的那一类也难以区分出来。

如果手工改名实在是我这样的懒人不想干的事,于是着手编写一个程序来批量改名。

程序功能:按给定条件将一个目录内(不含子目录)的所有文件名进行更改。

目前实现的改名方式有:"将文件名前添加相同的前缀","把文件名中的指定字符替换成其它字符","删除文件名中的特定字符","保留文件名中指定长度的字符","将文件名改为指定的序列"使用的编程语言是:C#首先新建一个:Windows窗口应用程序添加如下控件为comboBox1的items属性中添加如下内容"将文件名前添加相同的前缀","把文件名中的指定字符替换成其它字符","删除文件名中的特定字符","保留文件名中指定长度的字符","将文件名改为指定的序列"为comboBox1添加SelectedValueChanged事件private void comboBox1_SelectedValueChanged(object sender, EventArgs e){int iselect = comboBox1.SelectedIndex;switch (iselect){case 0://MessageBox.Show(comboBox1.Text);将文件名前添加相同的前缀 label2.Text = "添加的前缀";label3.Visible = false;textBox3.Visible = false;break;case 1:// MessageBox.Show(comboBox1.Text );把文件名中的指定字符替换成其它字符 label2.Text = "文件名中的字符";label3.Visible = true ;textBox3.Visible = true ;label3.Text = "要替换成的字符";break;case 2://MessageBox.Show(comboBox1.Text);删除文件名中的特定字符label2.Text = "要删除的字符";label3.Visible = false;textBox3.Visible = false;break;case 3://MessageBox.Show(comboBox1.Text);保留文件名中指定长度的字符label2.Text = "要保留的长度";label3.Visible = false;textBox3.Visible = false;break;case 4:// MessageBox.Show(comboBox1.Text);将文件名改为指定的序列break;case 5:MessageBox.Show(comboBox1.Text);break;default :break;}}执行修改按钮的代码如下:private void button2_Click(object sender, EventArgs e){DirectoryInfo directory = new DirectoryInfo(textBox1.Text);var Files = directory.GetFiles(); //获取文件列表string strPrefix = "";string strNew_character = "";string strFile_name = "";string strExpanded_name = "";string strNew_Filename = "";int iselect = comboBox1.SelectedIndex;int iLenght = 0;try{strPrefix = textBox2.Text.Trim();strNew_character = textBox3.Text.Trim();if (strPrefix.Length == 0){MessageBox.Show("请输入“"+label2+"”");return;}else{if (iselect == 3 ||iselect ==4)//用正则测定输入的是否是数字{string pattern = "(^-?\\d+$)|^(-?\\d+)(\\.\\d+)?$";Regex regex = new Regex(pattern);if (!regex.IsMatch(strPrefix )){MessageBox.Show("输入的不是数字!");return;}else{iLenght = int.Parse(strPrefix);}}}//遍历文件列表foreach (var file in Files){strExpanded_name = file.Extension;//获取文件的扩展名(后缀名)strFile_name = ;switch (iselect){case 0://MessageBox.Show(comboBox1.Text);将文件名前添加相同的前缀strNew_Filename = strPrefix + strFile_name;break;case 1:// MessageBox.Show(comboBox1.Text );把文件名中的指定字符替换成其它字符strNew_Filename = strFile_name.Replace(strPrefix, strNew_character);break;case 2://MessageBox.Show(comboBox1.Text);删除文件名中的特定字符strNew_Filename = strFile_name.Replace(strPrefix,"");break;case 3://MessageBox.Show(comboBox1.Text);保留文件名中指定长度的字符strNew_Filename = strFile_name.Substring(0, iLenght);break;case 4:// MessageBox.Show(comboBox1.Text);将文件名改为指定的序列break;case 5:MessageBox.Show(comboBox1.Text);break;default:break;}file.MoveTo(textBox1.Text + "\\" + strNew_Filename);//重命名后的文件保存在原来的目录下}MessageBox.Show("批量重命名成功!");}catch (Exception ex){MessageBox.Show(ex.Message);}}退出按钮代码为:private void button3_Click(object sender, EventArgs e){this.Close();}下面是重个程序的完整代码文件“Form1.Designer.cs”namespace rename{partial class Form1{///<summary>///必需的设计器变量。

WPF中往ComboBox里添加选项并指定默认选项

WPF中往ComboBox里添加选项并指定默认选项

WPF中往ComboBox里添加选项并指定默认选项这ComboBox里可以在XAML中直接给它添加选项,这里不直接废话,下面是在你要添加的ComboBox在XAML中的实现:1<ComboBox x:Name="MyComBox"Height="20"Canvas.Left="110"Canvas.To p="255"Width="86"DisplayMemberPath="Frequ"SelectionChanged="myComboB ox_SelectionChanged">注意代码里在ComboBox属性里的DisplayMemberPath="Frequ"这个是一种绑定也是填充选项的关键,要与C#里的定义的结构体的成员名字一致。

这个事件是当你选择ComboBox 里的选项时触发的事件SelectionChanged="myComboBox_SelectionChanged"。

接着是定义结构体:1public struct Frequency2{3public int ID{get;set;}4public string Frequ{get;set;}5}里面的ID可以不设置,但很重要,当我们选择选项的时候可以用index处理我们的选择,其中关于取到选项的索引很容易但要取到选项的内容好像不太容易。

1List<Frequency>list=new List<Frequency>();2Frequency freq=new Frequency();3for(int i=0;i<lFreq.Length;i++)4{5freq.ID=i;6freq.Frequ=lFreq[i].ToString();7list.Add(freq);8}50this.MyComBox.ItemsSource=list;//这里MyComBox是我们控件的ID51this.MyComBox.SelectedIndex=0;5253//在此点之下插入创建对象所需的代码。

ComboBox控件的用法教程

ComboBox控件的用法教程

ComboBox控件的⽤法教程前⾯我们了解了ListBox(列表框)控件的使⽤,在中还有⼀个与ListBox控件⼗分相似的控件——ComboBox 控件,也叫组合框。

组合框控件包括两个部分,⼀部分是上部可以输⼊列表项的⽂本框;另⼀部分是位于⽂本框下⽅的列表框,⽤于显⽰⽤户可以从中选择的项的列表 ComboBox 控件和ListBox 控件在功能上很相似,很多情况下,这两个控件是可以互换使⽤的,但是还是有某种特定的环境下只适合使⽤⼀种控件的情况。

通常,ComboBox控件适合于建议⽤户选择控件所列举的选项、同时⼜可以让⽤户⾃⾏在⽂本框中输⼊列表中不存在的选项的情况;⽽ListBox 控件适合于限制⽤户只能选择列表中的选项的情况。

在⽤户界⾯上,因为ComboBox 控件默认情况下是存在下拉列表框的,所以⽐ListBox 控件占⽤的窗体空间少,更加适合于使⽤在存在⼤量列表项的情况下。

如下图三所⽰的是QQ修改个⼈设置的界⾯,它⼤量地合理使⽤ComboBox 控件,使得⽤户界⾯简洁且能容纳下更多的选项信息。

由此可见,ComboBox 控件的⽐ListBox 控件更加能灵活多⽤。

⼀、ComboBox 控件的常⽤属性: 1、BackColor 属性:获取或设置ComboBox 控件的背景⾊。

2、DropDownStyle 属性:获取或设置指定组合框样式的值,确定⽤户能否在⽂本部分中输⼊新值以及列表部分是否总显⽰。

它包含三个值,默认值为 DropDown,如下表所⽰:成员名称说明DropDown ⽂本部分可编辑。

⽤户必须单击箭头按钮来显⽰列表部分。

DropDownList ⽤户不能直接编辑⽂本部分。

⽤户必须单击箭头按钮来显⽰列表部分。

Simple ⽂本部分可编辑。

列表部分总可见。

3、DropDownWidth 属性:⽤于获取或设置组合框下拉部分的宽度(以像素为单位),有些列表项太长,则需要通过改变该属性来显⽰该类表项的全部⽂字,如果未设置 DropDownWidth 的值,该属性返回组合框的 Width。

C#WinForm_ComboBox数据绑定的问题

C#WinForm_ComboBox数据绑定的问题

Visual Studio C#中的数据绑定五.复杂型组件的数据绑定:在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。

首先来介绍一下ComboBox组件的数据绑定.(1).ComboBox组件的数据绑定:在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、"DisplayMember"、"ValueMember"。

其中"DataSource"是要显示的数据集,"DisplayMember"是ComboBox 组件显示的字段,"ValueMember"是实际使用值。

具体如下:ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;注释:此时绑定是Access 2000数据库中"person"表的"xm"字段。

由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){file://创建一个OleDbConnectionstring strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , "person" ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}图03:对ComboBox组件数据绑定的程序界面得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:public class Form1 : Form{private ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private ponentModel.Container components = null ;public Form1 ( ){file://打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;}file://清除程序中使用过的资源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void GetConnect ( ){// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;myConn.Open ( ) ;string strCom = " SELECT * FROM person " ;file://创建一个DataSetmyDataSet = new DataSet ( ) ;file://用OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file://把Dataset绑定person数据表myCommand.Fill ( myDataSet , " person " ) ;file://关闭此OleDbConnectionmyConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e ){ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = "person.xm" ;ComboBox1.ValueMember = "person.xm" ;}static void Main ( ){Application.Run ( new Form1 ( ) ) ;}}C# WinForm 中ComboBox数据绑定的问题2009-12-29 09:24怎样让WinForm中的ComboBox显示表中的一个字段,同时又绑定另一个字段?在Web中的ComboBox这样写可以绑定两个值:boBox1.DataTextField="B000602";//显示中文,方便用户选择boBox1.DataValueField="B000601";//绑定与选择对应的另一个值boBox1.DataBind();但是在WinForm程序中该怎么写啊?0******************************************************************DataSet ds = new DataSet();//这个DataSet是你从数据库里取出来的值string[] arr = new string[ds.Tables[0].Rows.Count];for (int i = 0; i < arr.Length; i++){arr[i] = ds.Tables[0].Rows[i][2].ToString();}textBox1.AutoCompleteCustomSource.AddRange(arr);textBox1.AutoCompleteSource =AutoCompleteSource.CustomSource;textBox1.AutoCompleteMode =AutoCompleteMode.SuggestAppend;1.******************************************************************* 假设combobox绑定的列表为DataSet2的ListTable表(含有ListID, ListName字段),需要绑定的记录字段为DataSet1的Table1表的ListID字段combobox.DataSource = dataset2.Tables["ListTable"]; combobox.DisplayMember = "ListName";combobox.ValueMember = "ListID";combobox.DataBindings.Add("SelectedValue", dataset1, "Table1.List ID");2.***************************************************************** //dt为数据表,ID,Name为dt的两个字段:comboBox1.DataSource = dt ;comboBox1.ValueMember ="ID";comboBox1.DisplayMember ="Name";3.******************************************************************SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where countr y='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds.Tables["Customers"];comboBox1.DisplayMember = "CompanyName";comboBox1.ValueMember = "CompanyName";++++++++++++++++或者++++++++++++++++++++++SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind"); SqlCommand cmd = con.CreateCommand();mandText = "Select * from Customers where countr y='USA'";SqlDataAdapter adp = new SqlDataAdapter();adp.SelectCommand = cmd;DataSet ds = new DataSet();adp.Fill(ds, "Customers");comboBox1.DataSource = ds;comboBox1.DisplayMember = "panyName";comboBox1.ValueMember = "panyName";++++++++++++往DataGrid里添加下拉列表++++++++++++ DataGridTableStyle dgt = new DataGridTableStyle();dgt.MappingName = "test";DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn(); dgtbc.MappingName = "name";dgtbc.HeaderText= "name";ComboBox cmbFunctionArea = new ComboBox(); cmbFunctionArea.DataSource = DtGeneral;cmbFunctionArea.DisplayMember = "name"; cmbFunctionArea.ValueMember = "value";cmbFunctionArea.Cursor = Cursors.Arrow;cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;cmbFunctionArea.Dock = DockStyle.Fill;dgtbc.TextBox.Controls.Add(cmbFunctionArea);dgt.GridColumnStyles.Add(dgtbc);cmbFunctionArea.SelectionChangeCommitted +=new EventHandler(cmbFunctionArea_SelectionChangeCommitted);+++++++++++++修改++++++++++++++++private void cmbFunctionArea_SelectionChangeCommitted(object se nder, EventArgs e){((DataTable)dataGrid1.DataSource).Rows[dataGrid1.CurrentRowIndex][0] = ((ComboBox)sender).Text.ToString();dataGrid1.SetDataBinding(DtGeneral,null);}4.******************************************************************* *****DataBindings是一般控件所具有的,是绑定数据源的某一个字段combobox.DataBindings.Add("要绑定控件的属性如下拉框的SelectedValue\Text", 数据源如dataset1, "导航路径如Table1.ListID");但是,DataBindings只能绑定一个字段,而绑定多个字段时典型的如列表控件Combobox、ListBox控件,需要键值对,这时就需要指定DataSource(实现IList接口就行),然后指定ValueMember、DisplayMember 。

C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果

C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果

C# Winform ComboBox 在输入内容时 会在下拉菜单中显示根据输入内容查询的结果将 ComboBox 的 AutoCompleteMode 属性置为SuggestAppend , AutoCompleteSource 属性置为 ListItems ,然 后给 ComboBox 随便塞几个子项,运行看效果。

扩展:AutoCompleteMode 允许有四种值 :Suggest 在ComboBox 中输入字符后, ComboBox 会自动展开,显示匹配的子项,输入行不受影响,需要自己输入后续 字符,或者在下拉框中点选完整子项。

App end:输入字符后,字符后会自动补充匹配内容 示),但是 ComboBox 不会展开。

按上下键可以在多个匹配内 容中切换。

SuggestA pp end:上述两种模式的组合。

AutoCompleteSource 属性,共有 9 种,指示自动完成将要在 其中进行查找的数据源。

常用的几种如下 :ListItems: 数据源为 ComboBox 的 Item 集合。

FileSystem: 文件系统。

例如输入 后会展开 下的目录列表(或 append 模式下的自动添加 )。

同样的,此数据源也支持文 件名的补全。

None:默认值,指示ComboBox 不使用自动功能。

(以反色显Customsource:自定义数据源。

选用此方式时必须在代码中指 定 ComboBox 的 AutoCompleteCustomSource 属性为你构建的 AutoCompletestringCollection 对象,否则不会生效。

AutoCompleteStringCollection 类似于 List ,将你的数据 add 进去即可。

[ 高质量编程 ]团队成员为您解答,请提出宝贵意见和建议。

谢谢!QQ:176229432补充回答 :首先我需要强调一点,使用拼音首字母检索时可能比较适合DropDownStyle=DropDownList 时。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

ComboBox.SelectedIndex的使用问题
1、任何时候,SelectedIndex变化都触发SelectedIndexChanged事件。

2.1、重新生成列表(先清空再添加)时:ComboBox显示空,但SelectedIndex保持不变。

2.2、重新绑定列表(ResetBinding)时:ComboBox会努力保持SelectedIndex不变;当SelectedIndex小于等于新列表的最大下标时,CombBox会显示新列表相应位置的项;但当SelectedIndex大于列表的最大下标时,ComboBox会先将SelectedIndex置为新列表的最大下标,然后再重新绑定列表,ComboBox显示新列表中新SelectedIndex处的值。

但千万不要以在这种情况下,SelectedIndexChanged 中就能获得与界面一致的值,因为ComboBox在绑定新列表之前重置SelectedIndex 的值,因此,你在SelectedIndexChanged中获得的只是旧列表的值。

3、在初次将列表绑定到数据源时:ComboBox会显示列表中第一个选项,但SelectedIndex仍然是-1。

由于SelectedIndex的以上特性,若ComboBox使用绑定列表并且列表会变化,则在使用SelectedIndexChanged事件实时将ComboBox的显示项映射到内部数据时,必须加以小心。

否则将极有可能内部数据与界面显示数据不一致。

我还尝试过使用SelectedValueChanged事件来代替SelectedIndexChanged事件,但是发现SelectedValueChanged事件与SelectedIndexChanged的唯一区别在于:当使用ComboBox列表绑定,用户尚未进行过选择操作时,SelectedValueChanged 会在ComboBox的显示值变化时被触发。

只要用户执行过选择操作,该事件就再也不会被触发。

猜测SelectedValueChanged应该只在SelectedIndex为-1的时候才有可能被触发。

最终,我还是只好老老实实的用SelectedIndexChanged。

为避免出现问题,建议:若要对ComboBox的列表使用绑定,则在初次绑定和重新绑定后,重置SelectedIndex为-1,然后将其置为一个默认值,或尝试将SelectedItem置为旧列表中的选中项。

测试证明SelectedItem设置成功时并且该项在新旧列表中位置不同时,SelectedIndexChanged会被触发。

注:2.1,2.2均指列表改变时的特征,2.2为使用数据绑定列表时的情况。

相关文档
最新文档