Making a listbox highlight items as you type?
Hi!
How can i make the listbox highlight the item as the user types the item text?
ie, if there is an item called "Apple" in the list and the user clicks on the listbox and starts typing "app..." it should highlight apple.
I thought this would be enabled by default but I guess it's not. It jumps to the item whose first character matches the last key stroke. Is this set through one of the extended listbox styles?:sick:
Re: Making a listbox highlight items as you type?
Hi,
Have you tried to set the AutoCompleteSource and AutoCompleteMode? It doesn't highlight but makes suggestions based upon what's in the box. Make sure the DropDownStyle property is set to DropDown and not DropDownList.
Re: Making a listbox highlight items as you type?
Hi,
thanks for your reply. I'm talking about a ListBox control however, not a ComboBox ;) those properties you mentioned are for combobox
Re: Making a listbox highlight items as you type?
Haha yes sorry. Apparently, I'm not drinking enough coffee.
There may be an easier way to do this but try the code below and see if that helps you out. The code is for the KeyPress event of the ListBox. It assumes there is string declared in your class named inputText. You will probably want to add additional code for handling other types of key presses. Hopefully this is what you had in mind...
Code:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// flag that the event has been handled. This prevents the autoselect.
e.Handled = true;
// check if the user hit the backspace key
if (e.KeyChar == (char)Keys.Back)
{
if (inputText.Length > 0)
inputText = inputText.Substring(0, inputText.Length - 1);
}
else
{
inputText += e.KeyChar.ToString();
// search the listbox items
int i = listBox1.FindString(inputText);
if (i != ListBox.NoMatches)
listBox1.SelectedIndex = i;
}
}
Re: Making a listbox highlight items as you type?
haha yea sometimes more coffee helps.
Thanks for the tip, I'll try it out if i don't find anything else, because I'm almost certain the listbox itself should support that, but alas no one seems to know how :cry:
Quote:
Originally Posted by wy125
Haha yes sorry. Apparently, I'm not drinking enough coffee.
There may be an easier way to do this but try the code below and see if that helps you out. The code is for the KeyPress event of the ListBox. It assumes there is string declared in your class named inputText. You will probably want to add additional code for handling other types of key presses. Hopefully this is what you had in mind...
Code:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// flag that the event has been handled. This prevents the autoselect.
e.Handled = true;
// check if the user hit the backspace key
if (e.KeyChar == (char)Keys.Back)
{
if (inputText.Length > 0)
inputText = inputText.Substring(0, inputText.Length - 1);
}
else
{
inputText += e.KeyChar.ToString();
// search the listbox items
int i = listBox1.FindString(inputText);
if (i != ListBox.NoMatches)
listBox1.SelectedIndex = i;
}
}
:afrog:
Re: Making a listbox highlight items as you type?
Yeah it would be a lot easier if the control itself supported that functionality somehow. If you do find such a solution make sure you post it.
Re: Making a listbox highlight items as you type?
I don't think the listbox does have this feature in windows forms. wy's method should work for you.
Re: Making a listbox highlight items as you type?
grr i thoguht there'd be a nice way :(
thanks wy124... i changed yoru code a bit to do the same thing. It clears the "search" after 2.5 seconds, and it also keeps this in the list boxs tag....
Code:
struct listMatch
{
public string text;
public int lastTick;
}
private void listBox_KeyPress(object sender, KeyPressEventArgs e)
{
ListBox lst = (ListBox)sender;
listMatch match;
if (lst.Tag == null || !(lst.Tag is listMatch) )
{
match.text = string.Empty;
match.lastTick = Environment.TickCount;
}
else
match = (listMatch)lst.Tag;
if (Environment.TickCount - match.lastTick > 2500)
match.text = string.Empty;
//Backspace
if (e.KeyChar == (char)Keys.Back && match.text.Length > 0)
match.text = match.text.Substring(0, match.text.Length - 1);
else if (char.IsLetter(e.KeyChar))
{
match.text += e.KeyChar;
int index = lst.FindString(match.text);
if (index != ListBox.NoMatches)
{
lst.SelectedItems.Clear();
lst.SelectedIndex = index;
}
}
match.lastTick = Environment.TickCount;
e.Handled = true;
lst.Tag = match;
}
Re: Making a listbox highlight items as you type?
If you make a hidden textbox (outside the screen with visible set to true), then use something like this, you can kinda make it work. I guess you'll need to add a timer to remove the text in the textbox after a second or so to make it function a bit better.
Code:
public Form1()
{
InitializeComponent();
listBox1.MouseUp += new MouseEventHandler(listBox1_MouseUp);
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Back)
{
textBox1.Text = "";
}
}
void listBox1_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Focus();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
listBox1.SelectedIndex = listBox1.FindString(textBox1.Text);
}
}
Edit: Updated code slightly