Can search for ComboBox ----

zhaozj2021-02-16  53

In the .NET technology forum, once I saw a friend sent a post, it is probably: If there are many records in the database to read into the ComboBox, it happens to be more than 1000 records. If you want to choose 500 The record, then you can't die? So, it is best to enter the code or other help symbols, you can find that record immediately.

To this end, I made a control searchcomboBox. Because my expression ability is limited, I don't start the program directly.

First, create a project hexudong_comboBox

Then add a class itemname, the specific code is as follows

ItemName.cs

Using system;

Namespace Hexudong_comboBOX {///

/// iTemname's summary description. /// 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 definecode) {_ID = id; _Code = code; _Name = name; _PinYinCode = pinyincode; _WuBiCode = wubicode; _DefineCode = definecode; _Text = _Code "" _Name;

///

/// ID number /// public long id {get {return _id;} set {_id = value;}}

///

////// public string code {get {return_code;} set {_code = value;}}

///

/// Name /// public string name {get {return_name;} set {_name = value;}}

///

/// Pinyin code /// public string pinyincode {get {return_pinyincode;} set {_pinyincode = value;}} /// /// 5 code /// public string wubicode {get {return_wubicode;} set {_wubicode = value;}}

///

/// Custom code /// public string definecode {get {return _definecode;} set {_definecode = value;}}

///

/// Control text /// public string text {get {return_text;} set {_text = value;}}

///

/// Override toString method /// /// public override string toString () {return_text;}}}

Add a class SearchComboBox, the specific code is as follows:

SearchComboBox.cs

Using system.windows.forms; using system.drawing;

Namespace Hexudong_comboBox {///

/// Searchcombbox Summary Description. /// public class searchcomboBox: system.windows.forms.comBobox {public searchcomboBOX () {// // Todo: Add constructor logic // DrawMode = DrawMode.OwnerDrawFixed;}

/ / Find the corresponding name value according to the Code of the input text box, and display the code name string protected override void onkeypress (keypressAndargs 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);

// Lost focus protected override void online {getText (false); base.onlostfocus (e);}

// get focus protected override void OnGotFocus (EventArgs e) {GetText (true); base.OnGotFocus (e);} // selection changes protected override void OnSelectedIndexChanged (EventArgs e) {GetText (true); base.OnSelectedIndexChanged (e }

///

/// Lost focus, get focus, select the text content /// /// Whether it is focused, mainly from the ONLOSTFOCUS event /// private string getText (Bool Focused) {if (SelectedItem! = NULL) {itemname item = (itemname) SelectedItem; if (fundused) {text = item.code " Item .Name; selectall ();} else {text = item.name;}} else {text = "";} Return TEXT;

// Heavy painting the content of the next puller, mainly to assign document 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);

///

/// Setting or get the ID number /// public long long selectedid {get {IF (selectditedItem! = null) {ItemName Item = (itemname) SelectedItem; Return Item.ID } Else {return -1;}}} set {INT i = 0; Foreach (Object Obj ITEMS) {ItemName Item = (itemname) Obj; if (item.id == value) {SELECTEDITEM = Item; Text = iTEM .Code " item.name; (== items.count-1) {SELECTEDITEM = NULL; Text =" ";} i ;}}} /// /// setting Or obtain the code //////////////////////////////////////i (selectdedItem! = NULL) {itemname item = (itemname) SelectedItem; Return item.code;} else {return ";}} set {INT i = 0; 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) {SELE CTEDITEM = NULL;} i ;}}}

///

/// Setting or get the name of the selection /// public string selectedname {get {{ipLECTEDITEM! = NULL) {itemname item. } Else {return "";}} set {INT i = 0; forward (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 compiled into class libraries HEXUDONG_COMBOBOX.DLL

Let's test the Hexudong_ComboBox.dll you just made.

Also create a test project, then add this hexudong_comboBox.dll to the toolbox

Drag one to the test interface FORM1, then you can add data to SearchComboBox in your code.

Some of the code in 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 like focused:

This is like this:

If you enter 003, then knock back to the car, then there will be

Ok, achieve function, end

Of course, I am still in the learning stage, maybe the above code is not very good, I hope to correct. Some features are not strong enough, please expand, thank you!

转载请注明原文地址:https://www.9cbs.com/read-22351.html

New Post(0)