Automatically filter out the match list item according to the content you entered, and display it in real time, and similar to the IE address bar. The procedure is very simple, but it is only a long time to update the blog, and this small article is used to update.
The program principle is this: Component inherits ComboBox, and has a listBox member. When the component's text changes, the matching list item is filtered from the ComboBox, and these list items are added to Listbox. Demonstration: using system;
Using system.collections;
Using system.componentmodel;
Using system.drawing;
Using system.data;
Using system.windows.forms;
Namespace XPANGLIB
{
///
/// AutoComboBox's summary description.
/// summary>
Public Class AutoComboBox: System.Windows.Forms.comBOBOX
{
Private system.windows.forms.listbox lbmain;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Public autoComboBox ()
{
/ / This call is required for the Windows.Forms Form Designer.
InitializationComponent ();
LBMAIN = New Listbox ();
LBMain.Click = New EventHandler (lbmain_click);
LBMain.KeyDown = new keyeventhandler (lbmain_keydown);
LBMain.visible = false;
}
///
// get a list of matching a given string
/// summary>
Private arraylist getfilllist (String Strvalue)
{
ArrayList Alreturn = New ArrayList ();
Int iCount = this.Items.count;
Ilen = strength;
For (int i = 0; i { String strselitem = this.items [i] .tostring (); Strselitem.Length CONTINUE; IF (strselitem.substring (0, ilen) == Strvalue) Alreturn.Add (Strselitem); } Return Alreturn; } /// /// Clean all the resources being used. /// summary> Protected Override Void Dispose (Bool Disposing) { IF (Disposing) { IF (Components! = NULL) Components.dispose (); } Base.dispose (Disposing); } #REGION component designer generated code /// /// Designer supports the required method - do not use the code editor // / Modify the content of this method. /// summary> Private vidinitiRizeComponent () {// // AutoComboBox // THIS.SORTED = TRUE; This.keyDown = new system.windows.Forms.KeyeventHandler (this.autocomboBox_keydown); this.dropdown = new system.eventhandler (this.autocomboBox_dropdown); THIS.TEXTCHANGED = New System.EventHandler (this.autocomboBox_textchanged); This.Selected Indexchanged = new system.eventhandler (this.autocomboBox_selected); This.Leave = New System.EventHandler (this.autocomboBox_leave); } #ndregion Protected Override Void OnPaint (Painteventargs PE) { // Todo: Add a custom painting code here // Call the base class onpaint Base.onpaint (PE); } Private void lbmain_click (Object Sender, System.EventArgs E) { IF (lbmain.selecteditems.count == 0) Return; String strasel = lbmain.selectedItem.toString (); THIS.TEXT = STRSEL; INT ISEL = this.findstringexact (text); IF (ISEL! = -1) this.selectedIndex = ISEL; LBMain.visible = false; } Private Void AutoComboBox_TextChanged (Object Sender, System.EventArgs E) { IF (Text == "") { LBMain.visible = false; Return; } IF (! this.parent.controls.contains (lbmain)) { Lbmain.width = this.width; LBMAIN.HEIGHT = 100; LBMain.Parent = this.parent; LBMain.top = this.top this.height 1; LBMain.Left = this.left; This.Parent.controls.add (lbmain); LBMain.BringTofront (); } ArrayList alfill = getFillList (text); LBMain.Items.clear (); LBMain.Items.addrange (alfill.toarray ()); IF (lbmain.Items.count> 0) lbmain.selectedIndex = 0; IF (! lbmain.visible) LBmain.visible = true; } Private Void AutoComboBox_leave (Object Sender, System.EventArgs E) { IF (! lbmain.focused) LBMain.visible = false; } Private void AutoComboBox_Dropdown (Object Sender, System.EventArgs E) { LBMain.visible = false; } Private void AutoComboBox_KeyDown (Object Sender, System.Windows E) { IF (lbmain.visible) { IF (e.keycode == keys.up || E.Keycode == Keys.down || E.Keycode == Keys.left || E.keycode == keys.right || E.keycode == keys.pagedown || E.Keycode == Keys.pageup) { LBMain_KeyDown (lbmain, e); e.handled = true; } Else if (e.keycode == keys.enter) { LBMAIN_CLICK (LBMAIN, E); e.handled = true; } } } Private void lbmain_keydown (Object Sender, System.Windows E) { IF (e.keycode == keys.up || e.keycode == keys.Left || E.Keycode == Keys.pageup) { IF (lbmain.selectedIndex> 0) LBMain.selectedIndex = lbmain.selectedIndex - 1; } Else if (e.keycode == keys.down || e.keycode == keys.right || E.Keycode == Keys.Pagedown) { IF (lbmain.selectedindex LBMain.selectedIndex = lbmain.selectedIndex 1; } } Private void AutoComboX_Selected Indexchanged (Object Sender, System.Eventargs E) { LBMain.visible = false; } } // End Class } // end namespace