在.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收集整理,歡迎訪問。