CodePudding user response:
Deliberately use the int. TryPase function,CodePudding user response:
Although difficult to use, can use tryParseCodePudding user response:
1, from the source to eliminate this error, do regular limit, let not the digital input is not in2, Int. J TryParse, returns false is an error
CodePudding user response:
Int result=0;Int. J TryParse (num, ref result);
CodePudding user response:
Using a regular expression, such asprivate void textBox1_Leave (object sender, EventArgs e)
{
if (! The Regex. IsMatch (textBox1. Text. The ToString (), @ ^ -?" \ [1-9] d * $| ^ 0 $"))
{
MessageBox. Show (" please enter the number ");
TextBox1. SelectAll ();
return;
}
}
Can only enter a negative integer, zero, positive integer, otherwise will prompt for Numbers
CodePudding user response:
If the web, there is a validation controls, dragged into a, just set properties directly and more convenientCodePudding user response:
Can be added for the input text box of a KeyPress events, only allows the user to enter Numbers and backspace, it must be an integer the wow
Private void TextBox1_KeyPress (object sender, KeyPressEventArgs e)
{
If (e.K eyChar! B='\' & amp; & ! Char. IsDigit (e.K eyChar))
{
E.H andled=true;
}
The else
{
MessageBox. Show (" please enter an integer ", "type Error," MessageBoxButtons. OK, MessageBoxIcon. Error);
}
}
CodePudding user response:
Come in and see learningCodePudding user response:
Listen to after the mean want the user to enter the building Lord, if it is not digital will prompt the user error,1. Limited user input number only
Or (2) without limiting content, put a label at the back of the textbox, hide this label first, when the textbox looses focus,
Judge int. TryParse (num, ref result); For number, if not, then modify the text label for "can only input Numbers", and displayed,
Or (3) online real-time query ajax validation user name,
CodePudding user response:
1, regular control input,2, int. J TryParse ()
Public static bool IsNumber (object sNum)
{
Long num.//temp
If (sNum==null)
{
Return false;//if the incoming value is NULL, return False
}
If (long. TryParse (sNum. ToString (), out num))//try to convert the incoming value
return true;//return True success
The else
Return false;//returns False failure
}
CodePudding user response: