Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Forms Code Examples

Make a Combo Box Auto Complete

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
Make a Combo Box Auto Complete If you want you VB Combo boxes to automatically complete then use the following code: 'API call (add to declaration section of form or module) 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 'Purpose : Auto completes a combo box during the KeyPress event, based on the text typed in by the user. 'Inputs : cboComplete The combo box to auto complete (completes the current text based on ' the listitems in the combo). ' KeyAscii The Ascii value of the key pressed (as supplied by the keypress event) ' [bLimitToList] If True, will only allow the user to enter values which are in the combos list. 'Outputs : Returns the KeyAscii value or zero if bLimitToList = True and the user types an invalid character. 'Notes : Call the routine from the _KeyPress event of the combo. ' The combo must be of a vbComboDropDown style, eg. KeyAscii = ComboAutoComplete(Combo1, KeyAscii, False) Public Function ComboAutoComplete(ByRef cboComplete As ComboBox, ByVal KeyAscii As Integer, Optional ByVal bLimitToList As Boolean = False) As Long Dim lRetVal As Long Dim sSearch As String Const CB_ERR = (-1), CB_FINDSTRING = &H14C On Error GoTo ErrFailed If cboComplete.Style <> vbComboDropdown Then Debug.Print "Error in ComboAutoComplete. Combo must be of the style vbComboDropdown..." Debug.Assert False 'Return the KeyAscii ComboAutoComplete = KeyAscii Exit Function End If If KeyAscii = 8 Then 'Pressed delete If cboComplete.SelStart <= 1 Then 'Last character, clear combo. cboComplete.Text = "" ComboAutoComplete = 0 Exit Function End If 'Delete text If cboComplete.SelLength = 0 Then 'Delete a single character sSearch = UCase$(Left$(cboComplete.Text, Len(cboComplete) - 1)) Else 'Delete the selected text sSearch = Left$(cboComplete.Text, cboComplete.SelStart - 1) End If ElseIf KeyAscii < 32 Or KeyAscii > 127 Then 'Invalid keyboard characters Exit Function Else 'Append the new text to the combo text If cboComplete.SelLength = 0 Then 'Append a character sSearch = UCase$(cboComplete.Text & Chr$(KeyAscii)) Else 'Insert a character sSearch = Left$(cboComplete.Text, cboComplete.SelStart) & Chr$(KeyAscii) End If End If 'Find the closest match lRetVal = SendMessage(cboComplete.hwnd, CB_FINDSTRING, -1, ByVal sSearch) If lRetVal = CB_ERR Then 'Did not find a matching item in list If bLimitToList = True Then 'Block the KeyAscii ComboAutoComplete = 0 Else 'Return the KeyAscii ComboAutoComplete = KeyAscii End If Else 'Found a matching item in list cboComplete.ListIndex = lRetVal cboComplete.SelStart = Len(sSearch) cboComplete.SelLength = Len(cboComplete.Text) - cboComplete.SelStart ComboAutoComplete = 0 End If Exit Function ErrFailed: 'Return the keycode ComboAutoComplete = KeyAscii End Function 'Demonstration: 'To call the ComboAutoComplete routine, place the following code in the form in the combo keypress event. Private Sub Combo1_KeyPress(KeyAscii As Integer) KeyAscii = ComboAutoComplete(Combo1, KeyAscii, False) End Sub