Jan 5, 2014

C# Make A TextBox Accept Only 1 (. or ,) with Digits

  • You First Have To Go To The Properties Of The Textbox And Find For The Event Keypress And Double Click The Event Then You Will Get A New Event Like Below ..


         private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
             {
            
              }


  • Now All You Have To Copy The Code Below And Paste It Inside The Event Function...


            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                    && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

----------------------------------------------------------------------------------------------------------------------------------
           // only allow one coma
            if (e.KeyChar == ',' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

  • And It Should Look Like This .. When Done.. 

            private void textbox1_KeyPress(object sender, KeyPressEventArgs e)

                {

                    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)&& e.KeyChar != '.')
                      {
                           e.Handled = true;
                      }

                      // only allow one decimal point
                      if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                     {
                         e.Handled = true;
                         }

                 }

No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...