Number Check And Decimal Control In C#

In this blog , I am going to explain the below topics,
  1. How to restrict user from entering alphabets and allow only numeric input
  2. Decimal Control (User input restriction after the decimal value)  
Restrict User input other than Numeric input keys
 
I have written the below code in common class to access across the project.
  1. public static bool numcheck(char chr)  
  2.        {  
  3.            /** 
  4.           Purpose    : to allow only numbers 
  5.           Assumptions: 
  6.           Effects    : 
  7.           Inputs     : character entered 
  8.           Returns    : True - character is number , False - Other than numbers 
  9.           **/  
  10.            bool blnRetVal = false;  
  11.            try  
  12.            {  
  13.                if (!char.IsControl(chr) && !char.IsDigit(chr))  
  14.                {  
  15.                    blnRetVal = true;  
  16.                }  
  17.            }  
  18.            catch (Exception ex)  
  19.            {                  
  20.            }  
  21.            return blnRetVal;  
  22.        }  
The above method can be called in keypress events. The below code shows you how to use the above method,
  1. private void txtPartCode_KeyPress(object sender, KeyPressEventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.                 e.Handled = clsCommonLibrary.numcheck(e.KeyChar);  
  6.             }  
  7.             catch (Exception ex)  
  8.             {  
  9.             }  
  10.         }  
Decimal Control

The below method is used to restrict user input after the decimal point based on the user input. I have written the below method in common class file so that I can use it throughout the project.This will avoid code repetition.
  1. public static bool decimalControl(char chrInput,ref TextBox txtBox , int intNoOfDec)  
  2.         {  
  3.             /** 
  4.             Purpose    : To control the decimal places as per user option 
  5.             Assumptions: 
  6.             Effects    : 
  7.             Inputs     :  
  8.             Returns    : None 
  9.             **/  
  10.             bool chrRetVal;  
  11.             try  
  12.             {  
  13.                 string strSearch =string.Empty;  
  14.   
  15.                 if (chrInput == '\b')  
  16.                 {  
  17.                     return false;  
  18.                 }                 
  19.                   
  20.                 if (intNoOfDec == 0)  
  21.                 {  
  22.                     strSearch = "0123456789";  
  23.                     int INDEX = (int)strSearch.IndexOf(chrInput.ToString());  
  24.                     if (strSearch.IndexOf(chrInput.ToString(), 0) == -1)  
  25.                     {  
  26.                         return true;  
  27.                     }  
  28.                     else  
  29.                     {  
  30.                         return false;  
  31.                     }  
  32.                 }  
  33.                 else  
  34.                 {  
  35.                     strSearch = "0123456789.";  
  36.                     if (strSearch.IndexOf(chrInput, 0) == -1)  
  37.                     {  
  38.                         return true;  
  39.                     }  
  40.                 }  
  41.   
  42.                 if ((txtBox.Text.Length - txtBox.SelectionStart) > (intNoOfDec) && chrInput == '.')  
  43.                 {  
  44.                     return true;  
  45.                 }  
  46.   
  47.                 if (chrInput == '\b')  
  48.                 {  
  49.                     chrRetVal = false;  
  50.                 }  
  51.                 else  
  52.                 {  
  53.                     strSearch = txtBox.Text;  
  54.                     if (strSearch != string.Empty)  
  55.                     {  
  56.                         if (strSearch.IndexOf('.', 0) > -1 && chrInput == '.')  
  57.                         {  
  58.                             return true ;  
  59.                         }  
  60.                     }  
  61.                     int intPos;  
  62.                     int intAftDec;  
  63.   
  64.                     strSearch = txtBox.Text;  
  65.                     if (strSearch == string.Empty) return false;  
  66.                     intPos = (strSearch.IndexOf('.', 0));  
  67.   
  68.                     if (intPos == -1)  
  69.                     {  
  70.                         strSearch = "0123456789.";  
  71.                         if (strSearch.IndexOf(chrInput, 0) == -1)  
  72.                         {  
  73.                             chrRetVal = true;  
  74.                         }  
  75.                         else  
  76.                             chrRetVal = false;  
  77.                     }  
  78.                     else  
  79.                     {  
  80.                         if (txtBox.SelectionStart > intPos)  
  81.                         {  
  82.                             intAftDec = txtBox.Text.Length - txtBox.Text.IndexOf('.', 0);  
  83.                             if (intAftDec > intNoOfDec)  
  84.                             {  
  85.                                 chrRetVal = true;  
  86.                             }  
  87.                             else  
  88.                                 chrRetVal = false;  
  89.                         }  
  90.                         else  
  91.                             chrRetVal = false;  
  92.                     }  
  93.                 }  
  94.             }  
  95.             catch (Exception ex)  
  96.             {  
  97.             }  
  98.             return chrRetVal;  
  99.         }  
The below code explains how to use a common method of decimal control. We can use this code in keypress events. 
  1. private void txtWeight_KeyPress(object sender, KeyPressEventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.                 e.Handled = clsRemanLibrary.numcheck(e.KeyChar);  
  6.                 e.Handled = clsRemanLibrary.decimalControl(e.KeyChar,ref txtWeight, 2);  
  7.             }  
  8.             catch (Exception ex)  
  9.             {  
  10.             }  
  11.         }  
Decimal Control Inputs
  1. Keychar - Input keys that we can take from keypress event arguments.
  2. Ref Text Box - Text Box name in which you need to control the decimal input.
  3. No Of Decimals - Number of decimals you need to allow after the decimal.. in the the above example I have passed 2 for the decimal number. 
So the system will allow the user to enter two numbers after the decimal points.. 
 
For example,

If input is 2 - 0.12
If input is 3 - 0.123