locked
How can i check if text box contain letters ? RRS feed

  • Question

  • I want to use IF statement so IF textbox1.text contain letters i mean if the user typed letters lets say abcde or adfgfghdfg whatever letters he type so the user will get some messagebox message .

     

    But only letters.

     

    How do i do it ?

     

    Thanks for help .

     


    danieli
    • Moved by Rudedog2 Wednesday, April 21, 2010 2:48 PM : WinForms issue (From:Visual C# General)
    Wednesday, April 21, 2010 2:30 PM

Answers

  • Hi, on the TextBox's KeyPress event, you can capture the input from the keyboard and filter for letters only. Then compare the character's ascii value to filter for letters.

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
          // will only accept letters or backspace keys
          if ((Convert.ToInt32(e.KeyChar) >= 65) && (Convert.ToInt32(e.KeyChar) <= 122))
          {
            
          }
          // if the key is anything other than a letter or a backspace, do not accept it
          else if ((Keys)e.KeyChar != Keys.Back)
          {
            e.Handled = true;
          }
        }

    • Proposed as answer by Natural_D Wednesday, April 21, 2010 5:30 PM
    • Marked as answer by Helen Zhou Thursday, April 22, 2010 6:33 AM
    Wednesday, April 21, 2010 2:40 PM
  • You can try using Regular Expression Validators.

    RegEx.IsMatch(theString,"^[A-Za-z]$")

    Where theString is your textbox.text value.

    • Proposed as answer by Natural_D Wednesday, April 21, 2010 5:30 PM
    • Marked as answer by Helen Zhou Thursday, April 22, 2010 6:33 AM
    Wednesday, April 21, 2010 2:43 PM

All replies

  • Hi, on the TextBox's KeyPress event, you can capture the input from the keyboard and filter for letters only. Then compare the character's ascii value to filter for letters.

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
          // will only accept letters or backspace keys
          if ((Convert.ToInt32(e.KeyChar) >= 65) && (Convert.ToInt32(e.KeyChar) <= 122))
          {
            
          }
          // if the key is anything other than a letter or a backspace, do not accept it
          else if ((Keys)e.KeyChar != Keys.Back)
          {
            e.Handled = true;
          }
        }

    • Proposed as answer by Natural_D Wednesday, April 21, 2010 5:30 PM
    • Marked as answer by Helen Zhou Thursday, April 22, 2010 6:33 AM
    Wednesday, April 21, 2010 2:40 PM
  • You can try using Regular Expression Validators.

    RegEx.IsMatch(theString,"^[A-Za-z]$")

    Where theString is your textbox.text value.

    • Proposed as answer by Natural_D Wednesday, April 21, 2010 5:30 PM
    • Marked as answer by Helen Zhou Thursday, April 22, 2010 6:33 AM
    Wednesday, April 21, 2010 2:43 PM
  • For questions related to WinForms and any WinForm control, including DataGridView use the Windows Forms General or Windows Forms Data Controls and Databinding forums.

    For questions related to Windows Presentation Foundation, please use the  Windows Presentation Foundation (WPF) forum.

    You should get more, faster, and better responses on the other forums.


    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, April 21, 2010 2:48 PM
  • Thanks .

     


    danieli
    Wednesday, April 21, 2010 3:02 PM