TheWizardofInt
Junior Contributor
I am trying to make a VB.Net form autofill from the contents of its drop down, so in this case if you type H and Hartford is in the drop down, then it autofills 'Hartford'
It finds the selection as you F-11 through the program, then when the sub closes it retypes the Hh and selects it
Any idea where I am messing up here?
Code:
' Constant
Public Const CB_SELECTSTRING = &H14D
' API Declarations
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer
Private Sub cmbCity_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbCity.KeyPress
Dim APIRetVal As Integer
Dim sText As String
Dim KeyAscii As String = e.KeyChar
With cmbCity
If Not (KeyAscii = Chr(8) Or KeyAscii = Chr(27) Or KeyAscii = Chr(13)) Then
If .SelectionLength > 0 Then
sText = Mid(.Text, 1, .SelectionStart) & KeyAscii
Else
sText = .Text & KeyAscii
End If
APIRetVal = SendMessage(.Handle, CB_SELECTSTRING, -1, sText)
If APIRetVal <> -1 Then
.SelectionStart = Len(sText)
.SelectionLength = Len(.Text) - .SelectionStart + 1
Else
.Text = sText
.SelectionStart = Len(sText) + 1
End If
KeyAscii = ""
End If
End With
End Sub
It finds the selection as you F-11 through the program, then when the sub closes it retypes the Hh and selects it
Any idea where I am messing up here?