Click here to Skip to main content
15,881,757 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.

I'm using keypress to make my textbox only allow letter, when i tried to insert capital letter using caps lock it's not working.

How do I make the coding to allow capital letter?

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:51pm
v2
Comments
King Fisher 16-May-15 3:50am    
Show off your code.

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 within the range of "a" to "z". Something like this:
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    e.Handled = !(e.KeyChar >= 'a' && e.KeyChar <= 'z');
    }

All you need to do is expand that to cope with upper case characters as well:
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    e.Handled = !((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'));
    }

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

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