Home > Net >  How do you determine the choice of input is an integer in c #
How do you determine the choice of input is an integer in c #

Time:10-10

Can automatic error is not the integer, the method how to get ah,,,, novice to solve,

CodePudding user response:

Deliberately use the int. TryPase function,

CodePudding user response:

Although difficult to use, can use tryParse

CodePudding user response:

1, from the source to eliminate this error, do regular limit, let not the digital input is not in
2, 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 as
 private 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 convenient

CodePudding 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 learning

CodePudding 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:

refer to 7th floor defrt4 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);
}
}


I also am such judgment

CodePudding user response:

Assume that the input integer as the num
If (num. GetTypeCode ()==TypeCode. Int32)
{
//if carrying out for the integer num
}
The else
{
//otherwise num is not as an integer
}
The integer is above

The return type, and then do judgment and specify the type of!

CodePudding user response:

You directly set a textbox can only input Numbers is not ok,,,,,,,,

CodePudding user response:

Try using modulus??

CodePudding user response:

Private bool IsNumeric (string STR)
{
If (STR==null | | STR. The Length==0)
{
Return false;
}
Var reg=new Regex (@ "^ \ +? 1-9] [0-9] * $");//positive integer
Var result=reg. IsMatch (STR);
return result;

}

CodePudding user response:

 
Bool isScs=int. TryParse (" S ", out int the result);
Bool isScs=int. TryParse (" 111 ", the out int the result);

Recommend this method,

CodePudding user response:

Use regular expressions to determine

CodePudding user response:

Judgment, if statements are available, and can have a try

CodePudding user response:

Use TryParse method
  •  Tags:  
  • C#
  • Related