How can I enter only numbers in a TextBox in WinForm (with decimal points)

Source: Internet
Author: User

Can take the form of Web Forms validation, using the TextChanged event of the textbox, whenever the contents of the TextBox changes, call the method validation of the regular expression, with a label after the text prompt input error, the specific code is as follows:

Private voidtextBox1_TextChanged (Objectsender, EventArgs e) {            if(TextBox1.Text.Trim ()! ="")            {                if(! Validate (TextBox1.Text.Trim (),@"^ (-?\d+) (\.\d+)? $") {Label1. Text="Please enter a number"; }                Else{Label1. Text="match correct"; }            }            Else{Label1. Text=""; }        }           /**////whether the specified regular expression is met        Static  Public BOOLValidate (stringStrstringregexstr) {Regex regex=NewRegex (REGEXSTR); Match Match=regex.            Match (str); if(match. Success)return true; Else                return false; }

Note To reference the namespaces: using System.Text.RegularExpressions;

How can I enter only numbers in a TextBox in WinForm (with decimal points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.