combobox that can be searched----

Source: Internet
Author: User
Tags foreach constructor gettext tostring trim
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
//
}

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)
{
_id=id;
_code=code;
_name=name;
_pinyincode=pinyincode;
_wubicode=wubicode;
_definecode=definecode;
_text=_code + "" + _name;
}

<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);
}

Lose focus
protected override void OnLostFocus (EventArgs e)
{
GetText (FALSE);
Base. OnLostFocus (e);
}

Get the focus
protected override void OnGotFocus (EventArgs e)
{
GetText (TRUE);
Base. OnGotFocus (e);
}

Select Item Change
protected override void OnSelectedIndexChanged (EventArgs e)
{
GetText (TRUE);
Base. OnSelectedIndexChanged (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 ();

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

Part of the code in the 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));
}

........

This is the time to focus:



It's like this when you lose your focus:



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!


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.