c#--control text box can only enter integers (including decimals)

Source: Internet
Author: User

//control controls Enter a positive number (including decimals)        Private voidTxt_weight_keypress (Objectsender, KeyPressEventArgs e) {            //The first step: Determine whether the input is a digital--char. Isnumber (E.keychar)//if it is a number, you can enter (E. Handled = false;)//if it is not a number, determine if it is a decimal point            if(Char. Isnumber (E.keychar)) {e.handled=false; }            Else            {                //determines whether the input is a decimal point, or a period in the Chinese state, or a backspace//if it is a decimal point, the loop determines whether each character is a decimal point, if there is no input if there is no input allowed//If the BACKSPACE key, allow input--if (E.keychar = = ' \b ')//if it is not a decimal point and is not a backspace key, the input is not allowed                if(E.keychar = = Convert.tochar (". ") || E.keychar = = Convert.tochar ("."))                {                    intI_d =0;  for(inti =0; i < txt_Weight.Text.Length; i++)                    {                        if(Txt_Weight.Text.Substring (I,1) ==".") {e.handled=true; I_d++; return; }                    }                    if(I_d = =0)                    {
E.keychar = Convert.tochar ("."); /Set the key input value to "." E.handled=false; } } Else if(E.keychar = ='\b') {e.handled=false; } Else{e.handled=true; } } }

c#--control text box can only enter integers (including decimals)

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.