textbox validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • annaduraidte
    New Member
    • Sep 2008
    • 3

    textbox validation

    i am developing a application .net2.0 c# windows application,
    when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Originally posted by annaduraidte
    i am developing a application .net2.0 c# windows application,
    when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others
    To start, this isn't the proper way to ask questions. Actually, you never asked a question, you just told us your requirements. Nobody will do your program for you.

    MODERATOR

    However, I can help you get started.

    As for moving to the next fields with the enter key, you will need to add an event handler to the textboxes' KeyDown event. Then check if the key was enter. The KeyEventArgs e should hold this data. Make a separate listener for each textbox, and on the case that enter is pressed, .Focus() on the next control.

    As for the numeric text box, you have a couple of options. You can replace your TextBox with a NumericUpDown control. That will take numeric input as well as giving you buttons to click up and down. You can use a MaskedTextBox, and apply a mask that will only accept numbers. You can have running validation in the textbox's TextChanged event. Or, you can do your validation in the same event where you check for the Enter key.

    Comment

    • shweta123
      Recognized Expert Contributor
      • Nov 2006
      • 692

      #3
      Hi,

      You need to check for following 2 things in your C# code for your problem :

      1> Check if enter key is pressed or not in the first textbox and write the necessary code if key pressed is Enter key. For this you have to write the following code in keypress event of that textbox.

      e.g.
      Code:
       
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
              {
                  int c= Convert.ToInt32(e.KeyChar);
                  
                  //check if enter key is pressed
                  if (c == 13)
                  {
                    //set focus on the next textbox
                      textBox2.Focus();
                  }
              }
      2> Check if text inputted into the textbox contains only numbers or not.
      You can try following function for doing that.

      Code:
      // function for checking if numeric value is input into the textbox
      bool MatchRegex(string input)
      {
          // Regex match
          RegexOptions   options = RegexOptions.None;
          Regex regex = new Regex(@"^[0-9]*$", options);
          
      
          // Check for match
          bool   isMatch = regex.IsMatch(input.ToString());
      
          if (isMatch)
          {
              //if value in the textbox is numeric
              return true;
          }
          else
          {
              //if value in the textbox is not numeric
              return false;
          }
      }

      Originally posted by annaduraidte
      i am developing a application .net2.0 c# windows application,
      when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others

      Comment

      Working...