views:

452

answers:

2

I'm developing a Desktop Search Engine in VB.NET I'm using a ComboBox to specify the search query (string). I want the ComboBox to remember and display recent queries. I also want the ComboBox to try and Autocomplete the queries as the user is typing.

What is the best way to implement this?

+1  A: 

This can no doubt be done more elegantly, but here's the basic principles (apologies for any syntax issues, I'm not much of a VB guy):

In the KeyUp event:

  1. Make sure the key in't a navigation key:
    if e.KeyCode <> Keys.Back [...]
    
  2. Search the items list for the text typed:
    idx = myCombo.FindString(myCombo.Text)
    
  3. Take the combo's found item:
    s = myCombo.GetItemText(idx) 
    
  4. insert it into the .Text property:
    myCombo.Text = s
    

Note that this would overtype everything the user entered (destroying case). You could improve this by appending the 'missing' part instead:

stringToAppend = s.SubString(myCombo.Text.Length)
myCombo.Text = myCombo.Text + stringToAppend

Finally, select the new text so they can keep typing:

myCombo.SelectionStart = myCombo.Text.Length - stringToAppend.Length
myCombo.SelectionLength = stringToAppend.Length

Geoff
A: 

You might also want to look at this from vbAccelerator.com, offered as a basic starting point for auto-completion in VB.NET. The vbAccelerator code is usually high quality.

MarkJ