(VB.NET 1.1) Windows Forms Listbox help

G

Guest

Hi,

I have a listbox with 3000 entries, all of 4 chars, and the user needs to be
able easily select dozens of them...

When the user is selecting items, they type for example ABB, and the listbox
first goes to A, then B, rather than searching for ABB.

Is there any way to setup the listbox so that the user can type for example
ABB and it will jump to ABB instead of going to AAAA, then BBBB

in other words I need the listbox to take up to 4 keystrokes and search for
the nearsest match in the list...

is there any way to do that - or is there a sample I can look at eanywhere?

thanks

Philip
 
C

ClayB

You can try handling the KeyPress event and searching for the item
there.

private string match = "";
private int location = -1;

void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\b')
{
location = -1;
match = "";
this.listBox1.SelectedIndex = 0;
}
else if (char.IsLetter(e.KeyChar))
{
match += e.KeyChar;
int i = this.listBox1.FindStringExact(match);
if (i > -1)
{
this.listBox1.SelectedIndex = i;
}
else if (location < this.listBox1.Items.Count - 1)
{
i = this.listBox1.FindString(match, location + 1);
if (i > -1)
{
location = i;
this.listBox1.SelectedItem = this.listBox1.Items;
}
}
else
{
location = -1;
match = "";
this.listBox1.SelectedIndex = 0;
}
}
e.Handled = true;
}

=====================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top