Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends , I have requirement on validations in windows application. Textbox allows only characters as first letter .

please any one help me.
Posted
Comments
Sauradipta Chaudhury 27-Aug-13 2:09am    
By characters do you mean only Alphabets?
Sergey Alexandrovich Kryukov 27-Aug-13 2:25am    
It makes no sense at all, but probably just because you are using wrong terminology. Anything you enter in a text box is "characters".
What do you mean, exactly?
—SA

Try adding this eventhandler to your TextBox and see if that helps. I am assuming that you want the first character in the textbox as an alphabet i.e. either a-z or A-Z.


VB
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim textBox As TextBox
    textBox = sender

    If (textBox.Text.Length > 0 And textBox.Text.Length = 1) Then
        Dim firstChar() As Char
        Dim ascii As Integer
        Dim isAlphabet As Boolean

        firstChar = textBox.Text.ToCharArray
        ascii = Convert.ToInt32(firstChar(0))

        If ((ascii <= 90 And ascii >= 65) Or (ascii <= 122 And ascii >= 97)) Then
            isAlphabet = True
        End If

        If (Not isAlphabet) Then
            TextBox1.Text = String.Empty
        End If
    End If

End Sub
 
Share this answer
 
v2
Comments
Sauradipta Chaudhury 27-Aug-13 4:48am    
Please mark as answer if you find it helpful.
what about Regular Expressions?

Try this code, it will check if the text in your textbox matches the pattern in the []-brackets (^ will check the first character, A-Z will only allow uppercase characters):
VB
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim textBox As TextBox
    textBox = sender

    If (textBox.Text.Length > 0 And textBox.Text.Length = 1) Then
       If Not Regex.IsMatch(textBox.Text, "^[A-Z]") then
          TextBox1.Text = String.Empty
       End If
    End If
End Sub


Remember to import the System.Text.RegularExpressions, otherwise the code won't work.

Greetings
 
Share this answer
 
v2

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