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 {///
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;
///
///
///
///
///
///
///
Add a class SearchComboBox, the specific code is as follows:
SearchComboBox.cs
Using system.windows.forms; using system.drawing;
Namespace Hexudong_comboBox {///
/ / 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 }
///
// 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);
///
///
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!