Iki
Iki

Reputation: 119

How to use ASCII code for Enter in vb.net

I am having a little problem getting my code to do what I want.

I want to prevent the user from using the Enter button when he/she is entering text into a text. The code I am using is:

 Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Asc(e.KeyChar) = 13 Then
        e.Handled = False
    Else
        e.Handled = True
        MsgBox("Error.")
    End If
End Sub

This not achieving my objective. Please how can I re-write this?

Upvotes: 0

Views: 12037

Answers (1)

PaulAd
PaulAd

Reputation: 162

I agree with Tim3880. You are indeed keeping the user from entering anything with his/her keyboard; except the enter value. Your code is okay; only wrongly arranged, friend.

Try this:

 Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Asc(e.KeyChar) = 13 Then
        e.Handled = True
        MsgBox("Error.")
    Else
        e.Handled = False
    End If
End Sub

Upvotes: 3

Related Questions