Editing keystrokes

gqstud79

Newcomer
Joined
Sep 28, 2002
Messages
11
I have a dropdown combo box on my form and I'm trying to prevent the user from entering a leading <space> into it. This is the code I'm using, but there seems to be something wrong with my logic:

If (cboStockName.Text) Like "" Then
If Asc(e.KeyChar) = 32 Then
e.Handled = True
End If
End If

I've tried debugging it and it never makes it into the first if statement when I enter a leading <spacebar> into the combobox.
 
You need to use the = comparison operator to compare the text
in the first if statement, like this:

Visual Basic:
If cboStockName.Text = "" Then

Or you could use its Length property:

Visual Basic:
If cboStockName.Text.Length = 0 Then
 
I actually tried to simplify my keypress event so that it wouldn't accept any characters in the combo box and it still wouldn't work. here is my code:

Private Sub cboStockName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboStockName.KeyPress
e.Handled = True
End Sub


Any ideas what could be causing this problem?
 
Back
Top