In the. NET Technology Forum, once saw a netizen sent a post, probably means: If the database has a lot of records read to the ComboBox, just is greater than 1000 records, if you want to select the No. 500 record, that should not be bored to death ah? So, It is best to enter the code or other mnemonic symbols to find the record immediately.
To do this, I made a control searchcombobox. Because I have a limited ability to express, not good, I started the program directly
First, establish a project Hexudong_combobox
Then add a class ItemName, the specific code below
ItemName.cs
Using System;
Namespace Hexudong_combobox
{
<summary>
Summary description of the 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: Add constructor logic here
//
}
<summary>
ID number
</summary>
Public long ID
{
Get
{
return _id;
}
Set
{
_id=value;
}
}
<summary>
Code
</summary>
public string Code
{
Get
{
return _code;
}
Set
{
_code=value;
}
}
<summary>
Name
</summary>
public string Name
{
Get
{
return _name;
}
Set
{
_name=value;
}
}
<summary>
Pinyin code
</summary>
public string Pinyincode
{
Get
{
return _pinyincode;
}
Set
{
_pinyincode=value;
}
}
<summary>
Wubi Code
</summary>
public string Wubicode
{
Get
{
return _wubicode;
}
Set
{
_wubicode=value;
}
}
<summary>
Custom code
</summary>
public string Definecode
{
Get
{
return _definecode;
}
Set
{
_definecode=value;
}
}
<summary>
Control text
</summary>
public string Text
{
Get
{
return _text;
}
Set
{
_text = value;
}
}
<summary>
Overriding the ToString Method
</summary>
<returns></returns>
public override string ToString ()
{
return _text;
}
}
}
Add a class Searchcombobox, the specific code is as follows:
SearchComboBox.cs
Using System;
Using System.Windows.Forms;
Using System.Drawing;
Namespace Hexudong_combobox
{
<summary>
Summary description of the Searchcombbox.
</summary>
public class SearchComboBox:System.Windows.Forms.ComboBox
{
Public Searchcombobox ()
{
//
TODO: Add constructor logic here
//
DrawMode = drawmode.ownerdrawfixed;
}
Find the corresponding name value based on the code of the contents of the text box and display it as a string of codes + names
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);
}
<summary>
Lose focus, get focus, choose text content when changing
</summary>
<param name= "focused" > focus, mainly different from OnLostFocus events </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;
}
Redraw the contents of a Drop-down subkey, mainly by assigning text content
protected override void OnDrawItem (DrawItemEventArgs e)
{
E.drawbackground ();
E.drawfocusrectangle ();
<summary>
Sets or gets the ID number of the selected item
</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>
Sets or gets the code for the selected item
</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>
Sets or gets the name of the selected item
</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++;
}
}
}
}
}
Finally, compile into class library Hexudong_combobox.dll
Now let's test the Hexudong_combobox.dll.
Create another test project and add this hexudong_combobox.dll to the Toolbox
Drag one onto the test interface Form1, and then you can add data to the Searchcombobox in the code
If you enter 003 and then hit enter, then there will be
Okay, it's done, it's over.
Of course, I am still in the study stage, perhaps the above code is not very good, I hope to correct. Some features are not strong enough, please expand, thank you!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.