user3767883
user3767883

Reputation: 53

e.Keycode not act as expected

( sorry if title not clear enough )

in visual studio 2013 i wrote below code in ComdoBox key up

combobox data source set to odbc data table

I'm trying to make combobox act with Autocomplete text from it's items.

Dim PrK As Boolean = e.KeyCode = Keys.Back Or Keys.End Or Keys.Home _
                     Or Keys.PageDown Or Keys.PageUp Or Keys.Delete Or Keys.Up Or Keys.Down _
                     Or Keys.Right Or Keys.Left
If PrK = True Then
    Exit Sub
End If
Dim index As Integer
Dim Oldtext = cmbSubProName.Text
index = cmbSubProName.FindString(Oldtext)
If index > -1 Then
    Dim res As String
    res = cmbSubProName.Items(index).ToString
    cmbSubProName.SelectedIndex = index
    cmbSubProName.SelectionStart = Oldtext.Length
    cmbSubProName.SelectionLength = cmbSubProName.Text.Length
End If

when run project and press any key ( v, y or any other characters ) inside combo box I find Prk usually true !!

i also tried below code and the same

if e.KeyCode = Keys.Back Or Keys.End Or Keys.Home _
                     Or Keys.PageDown Or Keys.PageUp Or Keys.Delete Or Keys.Up Or Keys.Down _
                     Or Keys.Right Or Keys.Left then
exit sub
end if

Upvotes: 0

Views: 85

Answers (1)

Dan Puzey
Dan Puzey

Reputation: 34218

As a general rule, True is considered to be any non-zero value, and False is zero.

Your value for Prk is set incorrectly - you've got confused on your syntax. What you really want is this:

Dim PrK As Boolean = e.KeyCode = Keys.Back Or _
                     e.KeyCode = Keys.End Or _
                     e.KeyCode = Keys.Home Or _
                     e.KeyCode = Keys.PageDown Or _
                     e.KeyCode = Keys.PageUp Or _
                     e.KeyCode = Keys.Delete Or _
                     e.KeyCode = Keys.Up Or _
                     e.KeyCode = Keys.Down Or _
                     e.KeyCode = Keys.Right Or _
                     e.KeyCode = Keys.Left

The syntax as you've written it is actually doing a binary-Or of the values of all those key enumerations, which is always going to be a non-zero value.

Upvotes: 1

Related Questions