Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox question 3

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
My combobox consists of 3 items:
"Billable"
"Do Not Bill"
"No Charge"

I'd like to display Billable when the user enters "B" in the combobox. What if the user enters "Z"? Do I highlight "No Charge" or I don't highlight anything?

Do I have to write code to search for a matching item
(can anyone give me some sample, please?) or there is a simpler way?

Thank you.
 
this is code i got of tek tips a few weeks back (i think) however i cant find the actual thread!!
Code:
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any _
) As Long

Private Const CB_FINDSTRING = &H14C

Private Sub Combo1_KeyPress(KeyAscii As Integer)

    Dim lngRow As Long
    Dim strSearchText As String

    'Only process key if it is a character
    If KeyAscii < 32 Or KeyAscii > 127 Then
    Exit Sub
    End If

    If Combo1.SelLength = 0 Then
        strSearchText = Combo1.Text
    Else
        strSearchText = Left(Combo1.Text, Combo1.SelStart)
    End If
    
    strSearchText = strSearchText & Chr(KeyAscii)

    lngRow = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal strSearchText)

    If lngRow > -1 Then
        Combo1.ListIndex = lngRow
        Combo1.SelStart = Len(strSearchText)
        Combo1.SelLength = Len(Combo1.Text) - Combo1.SelStart
    End If
    
    KeyAscii = 0
    
End Sub

Private Sub Form_Load()

    Combo1.AddItem &quot;Baker&quot;
    Combo1.AddItem &quot;Barber&quot;
    Combo1.AddItem &quot;Barker&quot;
    Combo1.AddItem &quot;Berkley&quot;
    Combo1.AddItem &quot;Brown&quot;
    Combo1.AddItem &quot;Carter&quot;
    Combo1.AddItem &quot;Davis&quot;

End Sub
hope it helps

good luck!!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Why can't you just make it a drop down list and select one as a default? the user can scroll the box using the mouse or the up/down keys.

mick.
 
oldwiseone -
I'm selecting one as a default, but there are times when I want to leave it blank and the user must make a selection. I want the user to be able to do it fast, by typing one letter, and I fill the rest.
 
EwS

I think what oldwise one meant was change the style of your combo box to 2 (dropdown list).

If it is a dropdown list then by default the combo box will respond to the user input excatly how you want.

i.e. &quot;I'd like to display Billable when the user enters &quot;B&quot; in the combobox.&quot;
 
I misunderstood oldwiseone. This is exactly what I need.
Thanks!
 
The only problem that I'm having is that the Text property is read only, so I can't do this: Combo1.Text = &quot;XXX&quot;, nor this: Combo1.Index = 1.
 
you could try the following code as an example
you would have to change the combo1 name to the name of your combobox

Private Sub Combo1_Change()
Dim mystring As String
mystring = Combo1.Text
mystring = UCase(mystring) 'change to uppercase
mystring = Mid(mystring, 1, 1) 'select first letter of string

If mystring = &quot;B&quot; Then
Combo1.Text = &quot;Billable&quot;
ElseIf mystring = &quot;D&quot; Then
Combo1.Text = &quot;Do Not Bill&quot;
ElseIf mystring = &quot;N&quot; Then
Combo1.Text = &quot;No Charge&quot;
Else
Combo1.Text = &quot;&quot; 'or any default message
End If

End Sub

Hope this helps
 
EwS

You may find this more versatile. Only problem is you will need to get the user input from a Text Box.

Paste the code into Text1_Change event to try it.

For n = 0 To combo1.ListCount - 1
If InStr(1, Left(combo1.List(n), Len(Text1)), Text1, 1) Then
combo1.ListIndex = n
Exit For
End If
Next n

The advantage of this is that it is not case sensitive and it allows the user to enter more than just the first character, so for combo or list boxes that have lots of entries beginning with the same character, they can narrow the search by including the second, third fourth characters etc.

 
EwS

I have just realised what you are trying to do:-

> The only problem that I'm having is that the Text property is read only, so I can't do this:
> Combo1.Text = &quot;XXX&quot;, nor this: Combo1.Index = 1.

Combo1.Text = &quot;XXX&quot;, ------------- Should be Combo1.AddItem &quot;XXX&quot;
Combo1.Index = 1. ------------- Should be Combo1.ListIndex = 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top