在.net的技术论坛里,有一次看到了某网友发了个帖子,大概的意思就是:假如数据库中有很多的记录读取到combobox中,恰好是大于1000条记录,如果要选择其中第500条记录,那不得烦死了啊?所以,最好是输入代码或者其他的助记符号就马上可以找到那条记录.
为此,我作了一个控件searchcombobox.由于本人表达能力有限,不怎么好,就直接开始程序了
首先,建立一个项目hexudong_combobox
然后添加一个类itemname,具体代码如下
itemname.cs
using system;
namespace hexudong_combobox
{
 /// <summary>
 /// itemname 的摘要说明。
 /// </summary>
 public class itemname:object
 {
 private long _id;
 private string _code;
 private string _name;
 private string _pinyincode;
 private string _wubicode;
 private string _definecode;
 private string _text;
 public itemname()
 {
 //
 // todo: 在此处添加构造函数逻辑
 //
 }
 public itemname(long id,string code,string name)
 {
 _id=id;
 _code=code;
 _name=name;
 _text=_code + " " + _name;
 }
 public itemname(long id,string code,string name,string pinyincode,string wubicode)
 {
 _id=id;
 _code=code;
 _name=name;
 _pinyincode=pinyincode;
 _wubicode=wubicode;
 _text=_code + " " + _name;
 }
 public itemname(long id,string code,string name,string pinyincode,string wubicode,string definecode)
 {
 _id=id;
 _code=code;
 _name=name;
 _pinyincode=pinyincode;
 _wubicode=wubicode;
 _definecode=definecode;
 _text=_code + " " + _name;
 }
 /// <summary>
 /// id号
 /// </summary>
 public long id 
 {
 get
 {
 return _id;
 }
 set
 {
 _id=value;
 }
 }
 /// <summary>
 /// 代码
 /// </summary>
 public string code 
 {
 get
 {
 return _code;
 }
 set
 {
 _code=value;
 }
 }
 /// <summary>
 /// 名称
 /// </summary>
 public string name 
 {
 get
 {
 return _name;
 }
 set
 {
 _name=value;
 }
 }
 /// <summary>
 /// 拼音码
 /// </summary>
 public string pinyincode 
 {
 get
 {
 return _pinyincode;
 }
 set
 {
 _pinyincode=value;
 }
 }
 /// <summary>
 /// 五笔码
 /// </summary>
 public string wubicode 
 {
 get
 {
 return _wubicode;
 }
 set
 {
 _wubicode=value;
 }
 }
 /// <summary>
 /// 自定义码
 /// </summary>
 public string definecode 
 {
 get
 {
 return _definecode;
 }
 set
 {
 _definecode=value;
 }
 }
 /// <summary>
 /// 控件文本
 /// </summary>
 public string text 
 {
 get
 {
 return _text;
 }
 set
 {
 _text = value;
 }
  }
 /// <summary>
 /// 重写tostring方法
 /// </summary>
 /// <returns></returns>
 public override string tostring() 
 {
 return _text;
 }
 }
}
再添加一个类searchcombobox,具体的代码如下:
searchcombobox.cs
using system;
using system.windows.forms;
using system.drawing;
namespace hexudong_combobox
{
 /// <summary>
 /// searchcombbox 的摘要说明。
 /// </summary>
 public class searchcombobox:system.windows.forms.combobox
 {
 public searchcombobox()
 {
 //
 // todo: 在此处添加构造函数逻辑
 //
 drawmode = drawmode.ownerdrawfixed;
 }
 //根据输入文本框内容的code查找相应的名称值并显示为代码+名称的字符串
 protected override void onkeypress(keypresseventargs e)
 {
 if(e.keychar==(char)13)
 {
 foreach(object obj in items)
 {
 itemname item=(itemname)obj;
 if(item.code.trim()==text.trim())
 {
 selecteditem=item;
 text=item.code + " " + item.name;
 break;
 }
 }
 }
 base.onkeypress (e);
 }
 //失去焦点
 protected override void onlostfocus(eventargs e)
 {
 gettext(false);
 base.onlostfocus (e);
 }
 //得到焦点
 protected override void ongotfocus(eventargs e)
 {
 gettext(true); 
 base.ongotfocus (e);
 }
 //选择项改变
 protected override void onselectedindexchanged(eventargs e)
 {
 gettext(true);
 base.onselectedindexchanged (e);
 }
 /// <summary>
 /// 失去焦点,得到焦点,选择变化时的文本内容
 /// </summary>
 /// <param name="focused">是否聚焦,主要区别于onlostfocus事件</param>
 /// <returns></returns>
 private string gettext(bool focused)
 {
 if(selecteditem!=null)
 {
 itemname item=(itemname)selecteditem;
 if(focused)
 {
 text=item.code + " " + item.name;
 selectall();
 }
 else
 {
 text=item.name;
 }
 }
 else
 {
 text="";
 }
 return text;
 }
 //重画下拉子项的内容,主要是赋文本内容
 protected override void ondrawitem(drawitemeventargs e)
 {
 e.drawbackground();
 e.drawfocusrectangle();
 if (e.index < 0)
 e.graphics.drawstring("", e.font, 
 new solidbrush(e.forecolor), e.bounds.x, e.bounds.y);
 else
 { 
 if (items[e.index].gettype() == typeof(itemname)) 
 { 
 itemname item = (itemname)items[e.index];
 e.graphics.drawstring(item.text ,
 e.font,new solidbrush(e.forecolor),e.bounds.x,e.bounds.y);
 }
 else
 { 
 e.graphics.drawstring("", 
 e.font, new solidbrush(e.forecolor), e.bounds.x, e.bounds.y);
 }
 } 
 base.ondrawitem (e);
 }
 /// <summary>
 /// 设置或获取选择项的id号
 /// </summary>
 public long selectedid
 {
 get
 {
 if(selecteditem!=null)
 {
 itemname item=(itemname)selecteditem;
 return item.id;
 }
 else
 {
 return -1;
 }
 }
 set
 {
 int i=0;
 foreach(object obj in items)
 {
 itemname item=(itemname)obj;
 if(item.id==value)
 {
 selecteditem=item;
 text=item.code + " " + item.name;
 break;
 }
 if(i==items.count-1)
 {
 selecteditem=null;
 text="";
 }
 i++;
 }
 }
 }
 /// <summary>
 /// 设置或获取选择项的代码
 /// </summary>
 public string selectedcode
 {
 get
 {
 if(selecteditem!=null)
 {
 itemname item=(itemname)selecteditem;
 return item.code;
 }
 else
 {
 return "";
 }
 }
 set
 {
 int i=0;
 foreach(object obj in items)
 {
 itemname item=(itemname)obj;
 if(item.code.trim()==value.trim())
 {
 selecteditem=item;
 text=item.code + " " + item.name;
 break;
 }
 if(i==items.count-1)
 {
 selecteditem=null;
 }
 i++;
 }
 }
 }
 /// <summary>
 /// 设置或获取选择项的名称
 /// </summary>
 public string selectedname
 {
 get
 {
 if(selecteditem!=null)
 {
 itemname item=(itemname)selecteditem;
 return item.name;
 }
 else
 {
 return "";
 }
 }
 set
 {
 int i=0;
 foreach(object obj in items)
 {
 itemname item=(itemname)obj;
 if(item.name.trim()==value.trim())
 {
 selecteditem=item;
 text=item.code + " " + item.name;
 break;
 }
 if(i==items.count-1)
 {
 selecteditem=null;
 }
 i++;
 }
 }
 }
 }
}
最后,编译成类库hexudong_combobox.dll
下面来测试一下刚作的hexudong_combobox.dll
另外建立一个测试的项目,然后把这个hexudong_combobox.dll添加到工具箱中
拖一个到测试界面form1上,然后,就可以在代码中添加数据到searchcombobox中
form1.cs中的部分代码
.........
using hexudong_combobox;
.........
 
private void form1_load(object sender, system.eventargs e)
 {
 this.searchcombobox1.items.clear();
 users objusers=new userss().getusers();
 foreach(user objuser in objusers)
 {
 this.searchcombobox1.items.add(new itemname(objuser.id,objuser.code,objuser.name));
 }
........
聚焦的时候是这样的:
失焦的时候是这样的:
如果你输入003,然后敲回车,那么会出现
好了,实现了功能,结束了
当然,本人还是在学习阶段,或许上面的代码写的不怎么好,希望指正.有些功能还不够强,请扩展,谢谢!
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。