.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Code for allow decimal with BackSpace in TextBox ?

Posted By:Syed Shakeer Hussain       Posted Date: June 23, 2010    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Code to allow decimal with BackSpace in TextBox

Write the below code in KeyPress Event of TextBox Control

private void txtBox1_KeyPress(object sender, KeyPressEventArgs e)


   DecimaVaildation(e, txtBox1.Text );



 public void DecimaVaildation(KeyPressEventArgs e, string strtext)


//if the input char not a numeric value then dont allow that char...(backspace char and single decimal point is an exception for this)

       if ((int)e.KeyChar < 48 || (int)e.KeyChar > 57)


          bool bestcase = (int)e.KeyChar == 46 && ifDecimalNotFound(strtext);

          if (((int)e.KeyChar != '\b') && (!bestcase))

         e.Handled = true;




Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend