VB.NET - Numeric -Alphabets - Asked By sreya gopal on 14-May-12 07:59 AM

How can i write code or change the below code  so that my text box either takes numerical values or can take alphabets(A-Z),but alphanumerical values not allowed

 

'only numerical checking code

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

   

  
      If Not IsNumeric(e.KeyChar) And Not e.KeyChar = Chr(8) Then
        e.KeyChar = ""
Er:
        MsgBox("Only Numeric values allowed", vbCritical, Title:="Only Numerical Values")
      End If

      'Function for Handling  Dot when it is entered 2 times in the text box

      If (Char.IsControl(e.KeyChar) Or Char.IsDigit(e.KeyChar) = True) Then
        If txtSets.Text.IndexOf(".") <> -1 And e.KeyChar = "." Then
          e.Handled = True
        End If
      Else
        e.Handled = True
      End If

    End Sub

Sreenath Kalahasti replied to sreya gopal on 14-May-12 08:28 PM
Use the ascii table to check for your KeyChar values
Refer to this website for ascii values:
http://www.asciitable.com/ 

for example for digits 
use 
48 to 59 range while check for keychar.

Thanks
Sree
[)ia6l0 iii replied to sreya gopal on 14-May-12 09:33 PM
Modifed your code a bit.

Private Sub txtSets_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSets.KeyPress
   Dim currentCharacter As Char = e.KeyChar
    If Char.IsLetter(currentCharacter) Or Char.IsNumber(currentCharacter) Or Convert.ToByte (currentCharacter) = 8         Then
 ' Allow only alpha, numeric, and backspace
    Else
   MsgBox("Only Numeric values, alphabets are allowed", vbCritical, Title:="Only Numerical/alphabet Values") 
       e.Handled = True
    End If
  End Sub

Hope this helps.
Jitendra Faye replied to sreya gopal on 15-May-12 01:00 AM
I have created one example based on your need, try this example-

Dim flag As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      flag = ""
    End Sub


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
      Dim c = e.KeyChar
      If (flag <> "") Then
        If (flag = "ALPHA") Then
          If (IsNumeric(c)) Then
            MessageBox.Show("Only Complete Alphabet or Numeric are allowed.")
            e.Handled = True
          End If
        ElseIf (flag = "NUMERIC") Then
          If (Char.IsLetter(c)) Then
            MessageBox.Show("Only Complete Alphabet or Numeric are allowed.")
            e.Handled = True
          End If
        End If
      Else
        If IsNumeric(c) Then
          flag = "NUMERIC"
        ElseIf Char.IsLetter(c) Then
          flag = "ALPHA"
        Else
          MessageBox.Show("Only Complete Alphabet or Numeric are allowed.")
          e.Handled = True
        End If
      End If
    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
      If TextBox1.Text.Trim = "" Then
        flag = ""
      End If
    End Sub


TRy this code and let me know.
sreya gopal replied to Jitendra Faye on 15-May-12 01:56 AM
Almost near to my requirement,
But,if it is alphabet ,it must not allow more than one alphabet  (eg :A  not AC)
sreya gopal replied to Jitendra Faye on 15-May-12 02:10 AM
Thank you