Combo Box problems

Ive written code to make a combo box autoselect list items base on a series of key presses (e.g. 'n','a','n' would autoselect 'nancy' from a list which included 'neal',nadine,'ned'...etc.....
The algorithm detects the enter key to select the current text from the
combo box and populate a textbox with that data. (remember partial matches populate the combo box so enter is like the verifier saying 'this is the one I want'. THe problem I have is that I still want to be able to detect when the user selects something from the combobox's drop down using a mouse. Being that the mouse populates the combo box's text without any event other than "click" I am having trouble telling the difference between when the combobox is in an intermediate selection (user has only partially typed his item), and when the user has selected an item from the combo-box's drop down with the mouse.
here is the code which does this.
----------------------------------------------------------------------
Private Sub cmbProcedures_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Resume Next

strkey = KeyCode
strshift = Shift

If strkey = 40 And Shift = 4 Then
GoTo line_go
Else
If Not (strkey = 13) Then
Exit Sub
End If
End If

line_go:
strindex = cmbProcedures.ListIndex
cmbProcedures.Text = cmbProcedures.List(strindex)
End Sub
------------------------------------------------------------------------
Private Sub cmbProcedures_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "KEYCODE:" & KeyCode

If KeyCode = 13 Then
Procs
val = ""
Exit Sub
End If

val = Replace(val, Chr(16), "")
val = val & LCase(Chr(KeyCode))

If KeyCode = 8 Then
val = ""
Exit Sub
End If

For I = 0 To cmbProcedures.ListCount - 1
If (InStr(1, LCase(cmbProcedures.List(I)), val) = 1) Then
cmbProcedures.Text = cmbProcedures.List(I)
Exit Sub
End If
Next I
End Sub
------------------------------------------------------------------------
Private Sub cmbProcedures_LostFocus()
val = "" 'module level variable
End Sub
------------------------------------------------------------------------

Comments

  • : Ive written code to make a combo box autoselect list items base on a series of key presses (e.g. 'n','a','n' would autoselect 'nancy' from a list which included 'neal',nadine,'ned'...etc.....
    : The algorithm detects the enter key to select the current text from the
    : combo box and populate a textbox with that data. (remember partial matches populate the combo box so enter is like the verifier saying 'this is the one I want'. THe problem I have is that I still want to be able to detect when the user selects something from the combobox's drop down using a mouse. Being that the mouse populates the combo box's text without any event other than "click" I am having trouble telling the difference between when the combobox is in an intermediate selection (user has only partially typed his item), and when the user has selected an item from the combo-box's drop down with the mouse.
    : here is the code which does this.
    : ----------------------------------------------------------------------
    : Private Sub cmbProcedures_KeyDown(KeyCode As Integer, Shift As Integer)
    : On Error Resume Next
    :
    : strkey = KeyCode
    : strshift = Shift
    :
    : If strkey = 40 And Shift = 4 Then
    : GoTo line_go
    : Else
    : If Not (strkey = 13) Then
    : Exit Sub
    : End If
    : End If
    :
    : line_go:
    : strindex = cmbProcedures.ListIndex
    : cmbProcedures.Text = cmbProcedures.List(strindex)
    : End Sub
    : ------------------------------------------------------------------------
    : Private Sub cmbProcedures_KeyUp(KeyCode As Integer, Shift As Integer)
    : Debug.Print "KEYCODE:" & KeyCode
    :
    : If KeyCode = 13 Then
    : Procs
    : val = ""
    : Exit Sub
    : End If
    :
    : val = Replace(val, Chr(16), "")
    : val = val & LCase(Chr(KeyCode))
    :
    : If KeyCode = 8 Then
    : val = ""
    : Exit Sub
    : End If
    :
    : For I = 0 To cmbProcedures.ListCount - 1
    : If (InStr(1, LCase(cmbProcedures.List(I)), val) = 1) Then
    : cmbProcedures.Text = cmbProcedures.List(I)
    : Exit Sub
    : End If
    : Next I
    : End Sub
    : ------------------------------------------------------------------------
    : Private Sub cmbProcedures_LostFocus()
    : val = "" 'module level variable
    : End Sub
    : ------------------------------------------------------------------------
    :

    You need to tell whether the combo box was populated by click or by the enter key?

    You could set a form-wide switch (boolean variable) to true in the click event and then check it in the code that needs to know how the selection was made. If it is true then a choice was made by click. You could also set one for the key down event to be used in the same way. After you check the value for true or false, you set it back to false so it is initialized for the next time.

    James


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion