Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using Visual Studio 2012 and SQL Server 2012 to make register form.

When I'm using keypress to make my textbox only allow letter, the space button in my keyboard not working.

How do I make the coding for this?

my keypress coding:
VB
Private Sub txt_namaayah_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_namaayah.KeyPress
       If Not ((e.KeyChar >= "a" And e.KeyChar <= "z") Or e.KeyChar = vbBack) Then e.Handled = True
   End Sub

   Private Sub txt_namaibu_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_namaibu.KeyPress
       If Not ((e.KeyChar >= "a" And e.KeyChar <= "z") Or e.KeyChar = vbBack) Then e.Handled = True
   End Sub

   Private Sub txt_namawali_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_namawali.KeyPress
       If Not ((e.KeyChar >= "a" And e.KeyChar <= "z") Or e.KeyChar = vbBack) Then e.Handled = True
   End Sub


   Private Sub txt_namasiswa_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_namasiswa.KeyPress
       If Not ((e.KeyChar >= "a" And e.KeyChar <= "z") Or e.KeyChar = vbBack) Then e.Handled = True
   End Sub
Posted
Updated 16-May-15 18:50pm
v2
Comments
King Fisher 16-May-15 3:49am    
There is no use of reposting , pls remove your duplicate post and be patient for the answer :)

1 solution

At a guess - and without your code that's all it can be - you are testing the key press to see if it is a valid letter.Something like this:
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    e.Handled = !((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'));
    }

Or:
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    e.Handled = !Char.IsLetter(e.KeyChar);
    }

All you need to do is extend the "permitted characters":
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    e.Handled = !(Char.IsLetter(e.KeyChar) || e.KeyChar == ' ');
    }
 
Share this answer
 
Comments
Ralf Meier 16-May-15 11:09am    
I agree with OriginalGriff ...
Please note that the Spacebar isn't a letter ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900