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

上传人:b****1 文档编号:401039 上传时间:2022-10-09 格式:DOCX 页数:10 大小:18.82KB
下载 相关 举报
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第1页
第1页 / 共10页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第2页
第2页 / 共10页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第3页
第3页 / 共10页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第4页
第4页 / 共10页
C#WinformComboBox在输入内容时会在下拉菜单中显示根据输入内容查询的结果.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

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

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

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

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

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

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

扩展:

AutoCompleteMode允许有四种值:

None:

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

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;ll两次按键时间间隔

(单位:

毫秒)

privateclassProperties

{

publicStringBuilderPressedKeys;//按键

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("两次按键时间间隔(单位:

毫秒)")]publicintTwicePressKeyInterval

{

get

{

returnthis.twicePressKeyInterval;

}

set

{this.twicePressKeyInterval=value>0?

value:

0;

}

}

#endregion

#region私有函数

///

///确保扩展属性的菜单项在字典中

///

///目标菜单项

///扩展属性

privatePropertiesEnsurePropertiesExists(ListControlkey){

Propertiesp;

if(properties.ContainsKey(key)){

p=properties[key];

}else

{p=newProperties();

properties.Add(key,p);

}

returnp;

}

///

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

///

///汉字字符

///拼音首字母

privatestringGetFristPYLetterOfChar(stringc)

{

try

{

byte[]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<0xB0C5)return"a";

if(i<0xB2C1)return"b";

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";return"z";

}

catch

return"*"

}

}

///

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

///

///汉字字符串

///拼音首字母

privatestringGetFirstPYLetterOfString(stringstr){

StringBuildersb=newStringBuilder(100);foreach(charcinstr)

{

//字母和符号原样保留

if((int)c>=33&&(int)c<=126)sb.Append(c.ToString());

else

sb.Append(GetFristPYLetterOfChar(c.ToString()));}

returnsb.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

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1