none
Only numbers in textbox

    Question

  • Hello everyone, I want to create an application which will make user to Enter his/her data and will save in that data in database. For now, I want to create a textbox which will make user to enter his/her age. As age can only be an integer, So I want to create a textbox which accepts only numbers. I know I have to do this in some event for that textbox, but I don't know about that event. I also want User to restrict to enter age between 20 to 55. I also want to store the entry in database with the time at which that entry was made. I have already seen a thread on how can I Check the machine time. So for now I just want to create that textbox.
    Thursday, March 10, 2011 10:10 PM

Answers

  • Hello,

    Please read this article: http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:34 PM
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:34 PM
    Thursday, March 10, 2011 10:30 PM
  • Hello DCECoder, You can write code in textbox's KeyPress event to check whether the input is number or not, if not then handle that input. So, User will only be able to enter numbers in that textbox. If you want user to enter age between 20 to 55, then set textbox's maximum length property to 2. So, user will not be able to enter more that 2 numbers in that textbox.

    And about to check whether the User has entered value in 20 to 55, you have to write code in validating event for that textbox, which will produce an error if user has entered wrong age.

    And also, you must have one extra button which will let the user to close the form if he don't want to add an entry. Because in the following code, if user has entered wrong entry say less than 20, and leave that textbox, code will show him an error to enter value in between 20 to 55, and will send the control to textbox, so user will not be able to leave the textbox, even not allow the user to close the application. So you must have a control there which will let the user to cancel his entry and to close the application.

    Create an instance of ErrorProvider control in your application.

     

    // ------------------------------------------------------------------
    
    
    ErrorProvider er; // This will be used to show user error
    
    
           // when the user enter wrong age.
    
    
    // ------------------------------------------------------------------
    
    
    
    Enter the following code in your Form Load event.

    // ----------------------------------------------------------------
       textBox1.Validating += new CancelEventHandler(textBox1_Validating);
       textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
       textBox1.Leave += new EventHandler(textBox1_Leave);
       textBox1.MaxLength = 2;
    // Initializing ErrorProvider control
       er = new ErrorProvider(this);
       er.BlinkRate = 200;
       er.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;
    // -----------------------------------------------------------------
    Use the following code in various textbox events.

    // -------------------------------------------------------------------- 
    
      void textBox1_Leave(object sender, EventArgs e)
      {
       er.SetError(textBox1, string.Empty); // If user has entered right information
                 // then hide the ErrorProvider cotrol
      }
    
      void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
    //Checking whether the user has typed a number or backspace, if not then handle the event
       if (!Char.IsDigit(e.KeyChar) && !(e.KeyChar == 8))
       {
        e.Handled = true;
       }
      }
    
      void textBox1_Validating(object sender, CancelEventArgs e)
      {
    // Here we will be checking whther the user has entered right age or not
    // If not then we will show an error using ErrorProvider control
    // This will not allow user to leave textbox if he has entered wrong value
    
       float f;
       if (textBox1.Text != String.Empty)
       {    
        float.TryParse(textBox1.Text, out f);
        if (!(float.TryParse(textBox1.Text, out f)) || (f < 20.0f || f > 55.0f))
        {
         er.SetError(textBox1, "Only Enter Age between 20 to 55");
         e.Cancel = true;
        }
       }
      }
    // ----------------------------------------------------------------------
    Enter the following code in button click event. This button is to make the user close the application or leave the textbox when he is not able to leave the textbox, because he has entered wrong age.
    // -----------------------------------------------------------------------
       textBox1.Text = string.Empty;
       er.SetError(textBox1, string.Empty);

     

     


    How To Articles
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:32 PM
    Thursday, March 10, 2011 10:57 PM
  • When you leave a control and move to some other control, then various events takes place. Order of those events is

    Enter Event of 2nd control

    Got Focus Event of 2nd control

    Lost Focus of the 2nd control

    Validating event of 1st control

    Validated event of 1st control

     

    So, you can write code in Validating event to check whether there is any content in the 1st control, which is not valid. If there is some that kind of content, then you can cancel the validation, which will not move to the next event, which is Validated event. And if this validated event will not fire, .NET will not allow the user to leave the control, and will send the focus back to to the 1st control.

    Here is nice description regarding this event http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx


    How To Articles
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:29 PM
    Thursday, March 10, 2011 11:09 PM

All replies

  • Hello,

    Please read this article: http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:34 PM
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:34 PM
    Thursday, March 10, 2011 10:30 PM
  • Hello Cornel, that link has the information for WPF textbox, will that be same for Windows Form textbox. Sorry for this as I am not aware of WPF technology. And also I want to restrict user to enter only specific range of age. how to accomplish that task.
    Thursday, March 10, 2011 10:53 PM
  • Hello DCECoder, You can write code in textbox's KeyPress event to check whether the input is number or not, if not then handle that input. So, User will only be able to enter numbers in that textbox. If you want user to enter age between 20 to 55, then set textbox's maximum length property to 2. So, user will not be able to enter more that 2 numbers in that textbox.

    And about to check whether the User has entered value in 20 to 55, you have to write code in validating event for that textbox, which will produce an error if user has entered wrong age.

    And also, you must have one extra button which will let the user to close the form if he don't want to add an entry. Because in the following code, if user has entered wrong entry say less than 20, and leave that textbox, code will show him an error to enter value in between 20 to 55, and will send the control to textbox, so user will not be able to leave the textbox, even not allow the user to close the application. So you must have a control there which will let the user to cancel his entry and to close the application.

    Create an instance of ErrorProvider control in your application.

     

    // ------------------------------------------------------------------
    
    
    ErrorProvider er; // This will be used to show user error
    
    
           // when the user enter wrong age.
    
    
    // ------------------------------------------------------------------
    
    
    
    Enter the following code in your Form Load event.

    // ----------------------------------------------------------------
       textBox1.Validating += new CancelEventHandler(textBox1_Validating);
       textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
       textBox1.Leave += new EventHandler(textBox1_Leave);
       textBox1.MaxLength = 2;
    // Initializing ErrorProvider control
       er = new ErrorProvider(this);
       er.BlinkRate = 200;
       er.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;
    // -----------------------------------------------------------------
    Use the following code in various textbox events.

    // -------------------------------------------------------------------- 
    
      void textBox1_Leave(object sender, EventArgs e)
      {
       er.SetError(textBox1, string.Empty); // If user has entered right information
                 // then hide the ErrorProvider cotrol
      }
    
      void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
    //Checking whether the user has typed a number or backspace, if not then handle the event
       if (!Char.IsDigit(e.KeyChar) && !(e.KeyChar == 8))
       {
        e.Handled = true;
       }
      }
    
      void textBox1_Validating(object sender, CancelEventArgs e)
      {
    // Here we will be checking whther the user has entered right age or not
    // If not then we will show an error using ErrorProvider control
    // This will not allow user to leave textbox if he has entered wrong value
    
       float f;
       if (textBox1.Text != String.Empty)
       {    
        float.TryParse(textBox1.Text, out f);
        if (!(float.TryParse(textBox1.Text, out f)) || (f < 20.0f || f > 55.0f))
        {
         er.SetError(textBox1, "Only Enter Age between 20 to 55");
         e.Cancel = true;
        }
       }
      }
    // ----------------------------------------------------------------------
    Enter the following code in button click event. This button is to make the user close the application or leave the textbox when he is not able to leave the textbox, because he has entered wrong age.
    // -----------------------------------------------------------------------
       textBox1.Text = string.Empty;
       er.SetError(textBox1, string.Empty);

     

     


    How To Articles
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:32 PM
    Thursday, March 10, 2011 10:57 PM
  • Hey rt, what is that validating event?
    Thursday, March 10, 2011 11:02 PM
  • When you leave a control and move to some other control, then various events takes place. Order of those events is

    Enter Event of 2nd control

    Got Focus Event of 2nd control

    Lost Focus of the 2nd control

    Validating event of 1st control

    Validated event of 1st control

     

    So, you can write code in Validating event to check whether there is any content in the 1st control, which is not valid. If there is some that kind of content, then you can cancel the validation, which will not move to the next event, which is Validated event. And if this validated event will not fire, .NET will not allow the user to leave the control, and will send the focus back to to the 1st control.

    Here is nice description regarding this event http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx


    How To Articles
    • Marked as answer by DCEcoder Wednesday, March 16, 2011 6:29 PM
    Thursday, March 10, 2011 11:09 PM
  • Thanks rt, your code is working. Thanks for that help regarding Validating event. Your code has just two small error that you don't have made an entry to allow user to add decimal value. But I have made that change. And also for that to work, I have increased Max Limit of textbox to 4. And my program is now working.
    Thursday, March 10, 2011 11:27 PM