Combobox Auto Suggesting From A Button

TylerMorrison

Member
Joined
Aug 19, 2011
Messages
7
Programming Experience
Beginner
Hi guys, here's what I have. Essentially I have an onscreen keyboard on my form to allow for touch monitors. I have a combo box on the form, and when I hit the buttons that make up the touchscreen keyboard I drop the letter that the button corresponds to in the combo box. However, when I do this, the auto suggest feature doesn't work.

So for example, the combo box is called cbo, its items are:
"Apples"
"Alligators"
"Angels"
"Boys"
"Biscuits"
"Buoys"
"Balloons"
"Cobras"
"Cameras"
"Camel"

When I hit the button that corresponds with the letter c, I currently run the command cbo.text += "c", with checks to see if it it's a capital and other things, but essentially that's it. The combo boxes text now becomes c and that's all that happens. What I would like to happen is for the c to go into the text, which it currently does, but I would then like the box to auto suggest cobras, and for it to drop down and show cobras, cameras, and camel in the list.

The boxes AutoCompleteMode Property is currently set to SuggestAppend and it's AutoCompleteSource is set to ListItems.

Is there an easy way to achieve this? Any help is mucho appreciated.
 
Instead of cbo.text += "c", build a string with your button clicks and then use cbo.Select and SendKeys.Send(string) to invoke the Auto-Processing response of the keypress.

Firstly, thanks for the reply. So I'm assuming you mean do something along these lines.

VB.NET:
Dim str As String = cbo.Text
str += "c"
cbo.Select()
SendKeys.Send(str)

This code works exactly like I want if I have the AutoSuggestMode set to only suggest, however if it's set SuggestAppend, or I assume just Append, it also takes the suggestion, so if I typed C and Camel came up, it would take Camel as well, and the next letter goes on the end of the word. Example, I hit c, Camel comes up, I hit o and it now becomes Camelo.

To get around this I put a variable on to the form titled count, this variable reflects how many characters the user has typed, and I use that knowledge to get what they inputted instead of the entire string, the reflected code is below.

VB.NET:
 Dim str As String = Mid(ComboBox1.Text, 1, count + 1)
 str += "c"
 ComboBox1.Select()
 SendKeys.Send(str)
 count += 1

The only issue with this is that if the user uses a keyboard to delete or add characters my count doesn't get updated. Ideally no keyboard would be used with my program but Id rather not make it not work if they do use a keyboard, any thoughts on fixing this aspect?
 
You dont need to initialize str when you create it.

Create it on the form
Dim str As String

and then keep filling it and sending it each time another button-letter is pressed
str &= "c" ' or whatever letter
cbo.Select()
SendKeys.Send(str)


Works fine with SuggestAppend..

If you are concerned about a user using the keyboard then set the Form's KeyPreview property to True and handle the KeyPress event to amend your string
 
Back
Top