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

上传人:b****3 文档编号:3889289 上传时间:2023-05-06 格式:DOCX 页数:12 大小:76.73KB
下载 相关 举报
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第1页
第1页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第2页
第2页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第3页
第3页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第4页
第4页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第5页
第5页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第6页
第6页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第7页
第7页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第8页
第8页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第9页
第9页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第10页
第10页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第11页
第11页 / 共12页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx》由会员分享,可在线阅读,更多相关《C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx(12页珍藏版)》请在冰点文库上搜索。

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

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

C#WinformComboBox在输入内容时会在下拉菜单中显示

根据输入内容查询的结果

将ComboBox的AutoCompleteMode属性置为

SuggestAppend,AutoCompleteSource属性置为ListItems,然后给ComboBox随便塞几个子项,运行看效果。

扩展:

AutoCompleteMode允许有四种值:

Suggest在ComboBox中输入字符后,ComboBox会自动展

开,显示匹配的子项,输入行不受影响,需要自己输入后续字符,或者在下拉框中点选完整子项。

(以反色显

Append:

输入字符后,字符后会自动补充匹配内容

示),但是ComboBox不会展开。

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

SuggestAppend:

上述两种模式的组合。

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

常用的几种如下:

ListItems:

数据源为ComboBox的Item集合。

FileSystem:

文件系统。

例如输入后会展开下的目录列表

(或append模式下的自动添加)。

同样的,此数据源也支持文

件名的补全。

Customsource:

自定义数据源。

选用此方式时必须在代码中指

定ComboBox的AutoCompleteCustomSource属性为你构建的

AutoCompletestringCollection对象,否则不会生效。

AutoCompleteStringCollection类似于List,将你的数据add

进去即可。

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

谢谢!

QQ:

176229432

补充回答:

首先我需要强调一点,使用拼音首字母检索时可能比较适合

DropDownStyle=DropDownList时。

组件丢到其他UI中

,所有ComboBox和ListBox均会添加扩展属性SearchOnKeyEnable,置为true后自己看效果。

加入了按键间隔判断,两次按键时间间隔短则认为是一个词的数个字的首字母,间隔时间长认为是其他词的首字。

另外要注意,使用时请关闭AutoComplete相关属性,同时置

DropDownStyle=DropDownList效果最好。

此代码段仅供学习参考,望各位能自己写出更完善的代码而非直接拷贝使用。

usingSystem;

usingSystem.ComponentModel;

usingSystem.Collections.Generic;

usingSystem.Diagnostics;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data;

namespace!

#你的命名空间#!

//////ListControl扩展组件,使ListControl控件在按下键后能够自动搜索并定位到与按下键相匹配的项

Ill如在按下“s”后能自动定位到“深圳”///[ProvideProperty("SearchOnKeyPressedEnabled",typeof(ListControl))]publicpartialclassListControlExtender:

Component,

IExtenderProvider

#region私有字段

privateDictionaryproperties=newDictionary(7);

privateStringBuildersbKeys=newStringBuilder(10);

privateStringBuildersbItem=newStringBuilder(50);

privateinttwicePressKeyInterval=500;//两次按键时间间隔

privateclassPropertiespublicStringBuilderPressedKeys;//按键

publicDateTimePrevPressKeyTime;//上次按键时间

publicboolSearchOnKeyPressedEnabled;//按键搜索是否激活

publicProperties()this.PressedKeys=newStringBuilder(10);

this.PrevPressKeyTime=DateTime.Now;

this.SearchOnKeyPressedEnabled=false;

#endregion#region构造函数

publicListControlExtender()

InitializeComponent();

publicListControlExtender(IContainercontainer)container.Add(this);

InitializeComponent();

#endregion#region接口方法IExtenderProvider.CanExtendboolIExtenderProvider.CanExtend(objecto)if(oisListControl)returntrue;

returnfalse;

#endregion#region属性定义

[Category("设置")][Description("两次按键时间间隔(单位:

毫秒)")]publicintTwicePressKeyIntervalgetreturnthis.twicePressKeyInterval;

setthis.twicePressKeyInterval=value>0?

value:

0;

#endregion#region私有函数

///

///

privatePropertiesEnsurePropertiesExists(ListControlkey)

Propertiesp;

if(properties.ContainsKey(key))p=properties[key];

elsep=newProperties();

properties.Add(key,p);

returnp;

///

///

privatestringGetFristPYLetterOfChar(stringc)trybyte[]array=newbyte[2];

array=System.Text.Encoding.Default.GetBytes(c);

inti=(short)(array[0]-'\0')*256+((short)(array[1]-'\0'));

//if(i<0xB0A1)return"*";

if(i

if(i

0xB4EE)return"c";

if(i

0xB6EA)return"d";

if(i

0xB7A2)return"e";

if(i

0xB8C1)return"f";

if(i

0xB9FE)return"g";

if(i

0xBBF7)return"h";

if(i

0xBFA6)return"j";

if(i

0xC0AC)return"k";

if(i

0xC2E8)return"l";

if(i

0xC4C3)return"m";

if(i

0xC5B6)return"n";

if(i

0xC5BE)return"o";

if(i

0xC6DA)return"p";

if(i

0xC8BB)return"q";

if(i

0xC8F6)return"r";

if(i

0xCBFA)return"s";

if(i

0xCDDA)return"t";

if(i

0xCEF4)return"w";

if(i

0xD1B9)return"x";

if(i

0xD4D1)return"y";

//if(i<0xD7FA)return"z";

catch

 

return"*";

///

///

取得汉字字符串的拼音首字母

///

///

汉字字符串

///

拼音首字母

 

privatestringGetFirstPYLetterOfString(stringstr)

StringBuildersb=newStringBuilder(100);

foreach(charcinstr)//字母和符号原样保留if((int)c>=33&&(int)c<=126)sb.Append(c.ToString());

elsesb.Append(GetFristPYLetterOfChar(c.ToString()));

///

///

///

列表控件

///

集合索引

///

转换后的字符串

 

privatestringConvertListControlItemToString(ListControllistControl,intindex)if(listControlisComboBox)

//如果未绑定则直接转换为字符串if(listControl.DataSource==null)return(string)(((ComboBox)listControl).Items[index]);

else

//如果绑定则转换为DataRowView后再取相应字段值

DataRowViewview=(DataRowView)(((ComboBox)listControl).Items[index]);

if(((ComboBox)listControl).DisplayMember!

=null)return

Convert.ToString(view[((ComboBox)listControl).DisplayMember]);

elseif(((ComboBox)listControl).ValueMember!

=null)return

Convert.ToString(view[((ComboBox)listControl).ValueMember]);

elseelseif(listControlisListBox)

//如果未绑定则直接转换为字符串if(listControl.DataSource==null)return(string)(((ListBox)listControl).Items[index]);

else

//如果绑定则转换为DataRowView后再取相应字段值

DataRowViewview=(DataRowView)(((ListBox)listControl).Items[index]);

if(((ListBox)listControl).DisplayMember!

=null)return

Convert.ToString(view[((ListBox)listControl).DisplayMember])

elseif(((ListBox)listControl).ValueMember!

=null)return

Convert.ToString(view[((ListBox)listControl).ValueMember]);

else

 

else

///

///

privatevoidlistControl_KeyPress(objectsender,

KeyPressEventArgse)

ListControllistControl=(ListControl)sender;

//清空按键字符串

sbKeys.Remove(0,sbKeys.Length);

//取得当前时间

DateTimenow=DateTime.Now;

//取得与上次按下键时间差

TimeSpanspan=now-properties[listControl].PrevPressKeyTime;

//如果两次按键时间差小于TwicePressKeyInterval,则认为是按下多个键

if(span.TotalMilliseconds

sbKeys.Append(properties[listControl].PressedKeys.ToString())else//否则为按下一个键sbKeys.Append(e.KeyChar.ToString());

properties[listControl].PressedKeys.Remove(0,properties[listControl].PressedKeys.Length);

properties[listControl].PressedKeys.Append(sbKeys.ToString())

//更新按键时间properties[listControl].PrevPressKeyTime=now;

//记录初始索引值intoldIndex=listControl.SelectedIndex;

intnewIndex=oldIndex;

//取得列表项个数intitemCount=0;

if(listControlisComboBox)itemCount=((ComboBox)listControl).Items.Count;

elseitemCount=((ListBox)listControl).Items.Count;

//先从当前位置向后搜索boolmatch=false;

for(inti=oldIndex+1;i

//清空sbItem.Remove(0,sbItem.Length);

//取得列表项字符串sbItem.Append(ConvertListControlItemToString(listControl,i));

//比较按键与列表项首字母是否匹配

if(sbKeys.Length

GetFirstPYLetterOfString(sbItem.ToString()).IndexOf(sbKeys.ToString())==0;

elsematch=sbKeys.ToString().IndexOf(GetFirstPYLetterOfString(sbItem.ToString()))==0;

if(match)newIndex=i;

break;

if((newIndex==oldIndex)&&(oldIndex>0))//未搜索到则从0开始搜索

for(inti=0;i<=oldIndex;i++)

//清空sbItem.Remove(0,sbItem.Length);

//取得列表项字符串

sbItem.Append(ConvertListControlItemToString(listControl,i));

//比较按键与列表项首字母是否匹配if(sbKeys.Length

GetFirstPYLetterOfString(sbItem.ToString()).IndexOf(sbKeys.ToString())==0;

elsematch=sbKeys.ToString().IndexOf(GetFirstPYLetterOfString(sbItem.ToString()))==0;

if(match)newIndex=i;

break;

listControl.SelectedIndex=newIndex;

#endregion#region激活按下键时搜索功能

")]

[Description("是否激活按下键时搜索功能

[Category("扩展")]

publicboolGetSearchOnKeyPressedEnabled(ListControllistControl)

Propertiesp=EnsurePropertiesExists(listControl);

returnp.SearchOnKeyPressedEnabled;

publicvoidSetSearchOnKeyPressedEnabled(ListControllistControl,boolvalue)

Propertiesp=EnsurePropertiesExists(listControl);

p.SearchOnKeyPressedEnabled=value;

if(value)listControl.KeyPress+=new

KeyPressEventHandler(listControl_KeyPress);

elselistControl.KeyPress-=new

KeyPressEventHandler(listControl_KeyPress);

#endregion

最后,我还要指出一点,自动完成和首字母查询定位是不能混用的。

假设combobox子项为{"陕西","山东},当按下s键时,只可能展开combobox然后定位到"陕西",不可能也不应该同时更改combobox的显示字串。

输入的s可以删掉,但是无法决定我是该添'陕'字还是该添'山'字进去。

没法确定首字,也就没法

使用自动添加/完成了。

所以在此我只能建议你使用经典方式,也就是上面代码实现的功能:

dropdownlist,单击展开列表,按下键定位到合适的行;或者有焦点时直接输入拼音首字,在不展开列表的情况下直接填充。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2