In this blog , I am going to explain the below topics,
- How to restrict user from entering alphabets and allow only numeric input
- 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.
- public static bool numcheck(char chr)
- {
-
-
-
-
-
-
-
- bool blnRetVal = false;
- try
- {
- if (!char.IsControl(chr) && !char.IsDigit(chr))
- {
- blnRetVal = true;
- }
- }
- catch (Exception ex)
- {
- }
- return blnRetVal;
- }
The above method can be called in keypress events. The below code shows you how to use the above method,
- private void txtPartCode_KeyPress(object sender, KeyPressEventArgs e)
- {
- try
- {
- e.Handled = clsCommonLibrary.numcheck(e.KeyChar);
- }
- catch (Exception ex)
- {
- }
- }
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.
- public static bool decimalControl(char chrInput,ref TextBox txtBox , int intNoOfDec)
- {
-
-
-
-
-
-
-
- bool chrRetVal;
- try
- {
- string strSearch =string.Empty;
-
- if (chrInput == '\b')
- {
- return false;
- }
-
- if (intNoOfDec == 0)
- {
- strSearch = "0123456789";
- int INDEX = (int)strSearch.IndexOf(chrInput.ToString());
- if (strSearch.IndexOf(chrInput.ToString(), 0) == -1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- strSearch = "0123456789.";
- if (strSearch.IndexOf(chrInput, 0) == -1)
- {
- return true;
- }
- }
-
- if ((txtBox.Text.Length - txtBox.SelectionStart) > (intNoOfDec) && chrInput == '.')
- {
- return true;
- }
-
- if (chrInput == '\b')
- {
- chrRetVal = false;
- }
- else
- {
- strSearch = txtBox.Text;
- if (strSearch != string.Empty)
- {
- if (strSearch.IndexOf('.', 0) > -1 && chrInput == '.')
- {
- return true ;
- }
- }
- int intPos;
- int intAftDec;
-
- strSearch = txtBox.Text;
- if (strSearch == string.Empty) return false;
- intPos = (strSearch.IndexOf('.', 0));
-
- if (intPos == -1)
- {
- strSearch = "0123456789.";
- if (strSearch.IndexOf(chrInput, 0) == -1)
- {
- chrRetVal = true;
- }
- else
- chrRetVal = false;
- }
- else
- {
- if (txtBox.SelectionStart > intPos)
- {
- intAftDec = txtBox.Text.Length - txtBox.Text.IndexOf('.', 0);
- if (intAftDec > intNoOfDec)
- {
- chrRetVal = true;
- }
- else
- chrRetVal = false;
- }
- else
- chrRetVal = false;
- }
- }
- }
- catch (Exception ex)
- {
- }
- return chrRetVal;
- }
The below code explains how to use a common method of decimal control. We can use this code in keypress events.
- private void txtWeight_KeyPress(object sender, KeyPressEventArgs e)
- {
- try
- {
- e.Handled = clsRemanLibrary.numcheck(e.KeyChar);
- e.Handled = clsRemanLibrary.decimalControl(e.KeyChar,ref txtWeight, 2);
- }
- catch (Exception ex)
- {
- }
- }
Decimal Control Inputs
- Keychar - Input keys that we can take from keypress event arguments.
- Ref Text Box - Text Box name in which you need to control the decimal input.
- 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