TextBox Validation In Windows Forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Normann
    New Member
    • Jan 2007
    • 17

    TextBox Validation In Windows Forms

    Hallo

    I need some help with textboxes, I have a textbox that I need to test if it contains numbers only, I know I can use a masked textbox but it just do’s not fit my use, I have some of the code I need which you can see below here, the part I need is in the second line.

    Language: C#
    Platform: WinXP
    Programming software: VS2005
    Type: Windows Forms

    Code:
         
    
     if (textBoxAddBook_Year.Text.Length == 4)
       if (textBoxAddBook_Year.Text ==  Missing!!!)	
         if (Convert.ToInt32(textBoxAddBook_Year.Text) >= 1600)
           if (Convert.ToInt32(textBoxAddBook_Year.Text) <= DateTime.Now.Year)
              { varYearOK = true; }

    As you can see I need to check if the text in the textbox is numbers only before I check if the year is correct.

    NormannTheDane
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    There is a article on this.You may got some idea from that
    http://www.codeproject .com/useritems/Usertextboxcont rol.asp

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Add an ErrorProvider control to your form and use the Validated event:
      [code=c]
      private void textBox1_Valida ted(object sender, EventArgs e)
      {
      string msg = "";
      int iYear;
      if (textBox1.Text == "")
      msg = "Must be completed.";
      else if (textBox1.Text. Length != 4)
      msg = "Use 4 digit date.";
      else if (!int.TryParse( textBox1.Text, out iYear))
      msg = "Invalid year.";
      else if ((iYear < 1600) || (iYear > DateTime.Now.Ye ar))
      msg = "Year must be greater than 1600";
      errorProvider1. SetError(textBo x1, msg);
      }
      [/code]
      See the ErrorProvider help, http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx for a longer example.

      Comment

      • phvfl
        Recognized Expert New Member
        • Aug 2007
        • 173

        #4
        Hi Normann,

        You can accomplish the first two if conditions using one regular expression. You will need to add a Using statement to include System.Text.Reg ularExpressions and then the following will define a regular expression:
        Code:
        Regex reg = new Regex("^[0-9]{4}$");
        The content of the square brackets is the characters to match (0-9 is all numbers).

        The number in the curly brackets is the number of characters to match so this would match with 4 characters.

        The "^" matches with the start of the string and the "$" with the end of the string, these are used so that a string of 5 or more numbers is not matched with.

        You can then test for the match using the IsMatch member of the Regex object, the return is True if a match is found so your code could be written as:
        Code:
          if (reg.IsMatch(textBoxAddBook_Year.Text)){
            Int32 intYr = Convert.ToInt32(textBoxAddBook_Year.Text);
              if (intYr>=1600 && intYr <= DateTime.Now.Year){
              varYearOK = true; 
            }
          }
        I have also combined the next two if conditions as it looks tidier to read and only one conversion to an integer needs to be made.

        Comment

        • Normann
          New Member
          • Jan 2007
          • 17

          #5
          Ok ask a question, and not only get an answer to the question but also get a solution to three other problems, now that is what I like.

          Thank you all for your help.

          NormannTheDane

          Comment

          Working...