AutoComplete Combobox

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
In a visual basic.net how do I program a combobox to autocomplete from listeditems in a collection that are filled during the run of the application? Any help offered is greatly appreciated.
 
I got this from msdn some time ago. it's not perfect but it has worked great in windows forms for me.

Code:
public class DropDownBase : System.Windows.Forms.ComboBox
	{
		public DropDownBase()
		{			
			this.KeyUp+=new KeyEventHandler(ListKeyUp);			
			this.SelectedValueChanged+=new EventHandler(PhysicianChanged);
		}

		public void ListKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			int index;
			string actual;
			string found;

			if ((e.KeyCode == Keys.Back) ||
				(e.KeyCode == Keys.Left) ||
				(e.KeyCode == Keys.Right) ||
				(e.KeyCode == Keys.Up) ||
				(e.KeyCode == Keys.Down) ||
				(e.KeyCode == Keys.Delete) ||
				(e.KeyCode == Keys.PageUp) ||
				(e.KeyCode == Keys.PageDown) ||
				(e.KeyCode == Keys.Home) ||
				(e.KeyCode == Keys.End))
			{
				return;
			}

			
			actual = this.Text;

			
			index = this.FindString(actual);

			
			if (index > -1)
			{
				found = this.Items[index].ToString();

				
				this.SelectedIndex = index;

				
				this.SelectionStart = actual.Length;				
				this.SelectionLength = found.Length;
			}
		}
 
Back
Top