Sarfraz
Sarfraz

Reputation: 382776

How to allow only A-Za-z chars

I tried below code:

 Private Sub txtName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtName.KeyPress
  ' allow upper and lower case A-Z, and backspace
  If Not Chr(KeyAscii) Like "[A-Za-z]" And KeyAscii <> 8 Then KeyAscii = 0
 End Sub

But it gives:

'KeyAscii' is not declared. It may be inaccessible due to its protection level.

Any idea on how to allow alphabet only ?

Upvotes: 1

Views: 1743

Answers (5)

gen
gen

Reputation: 1

If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
 Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If

Upvotes: 0

gwt
gwt

Reputation: 2413

You should use the following Regular expression :

Dim reg_exp As New RegExp
reg_exp.Pattern = "^[a-zA-Z]*$"
If reg_exp.Test(txtName.Text.Trim()) Then
   MessageBox.Show("Input name is correct")
Else
   MessageBox.Show("Input name is not correct")
End If

Upvotes: 2

PsychoMantis
PsychoMantis

Reputation: 1015

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As     System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

Dim keyAscii As Char
keyAscii = e.KeyChar

        If keyAscii > Chr(64) And keyAscii < Chr(91) Then
        'char is A-Z

    End If

    If keyAscii > Chr(96) And keyAscii < Chr(123) Then
        'char is a-z
    End If
End Sub

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545696

It looks like you tried to translate a VB6 code verbatim. You need to re-learn the language, VB.NET is completely different in anything but name.

In your particular case, KeyAscii has been replaced by the KeyPressedEventArgs which has two members: KeyChar and Handled.

Furthermore, .NET distinguishes between characters and strings (= collection of characters), you cannot simply take a character and apply the Like operator to it, nor should you.

Instead, do the following:

If Character.IsLetter(e.KeyChar) Then
    e.Handle = True
End If

Setting Handled to True has generally the same effect as setting KeyAscii to 0 in VB6 (read the documentation!).

Furthermore, since you’re obviously just switching, make sure to enable both Option Explicit and Option Strict in the project options, as well as making it the default for further projects in the Visual Studio settings. This helps catching quite a lot of errors for you.

Finally, this code is bad for usability. It’s generally accepted that fields should not constrain user input in such a way (and it’s also not safe: what if the user uses copy&paste to enter invalid text?). Instead, you should test the validity of the input in the textbox’ Validating event, since it exists this very purpose.

Upvotes: 5

LeigerGaming
LeigerGaming

Reputation: 508

Where is KeyAscii defined? It looks like it's out of scope.

As you can't just pass it in as a parameter, try declaring it globally first. Then you should be able to access it inside your method.

Disclaimer: Whilst I've used it before, my knowledge of VB.NET is very limited.

Upvotes: 1

Related Questions