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.