问题描述
我很新的C#和这个问题听起来很愚蠢。我不知道我要如何从 textBox1的
获得整数(用户输入),如果else语句使用它?
I am very new to C# and this question might sound very stupid. I wonder how I'm going get the integer(user's input) from the textBox1
and use it in if else statement?
请举一些例子
推荐答案
您需要解析的值 textbox.Text
这是一个字符串 INT
值。您可以使用,或或的。
You need to parse the value of textbox.Text
which is a string to int
value. You may use int.TryParse, or int.Parse
or Convert.ToInt32
.
TextBox.Text
属性是字符串
键入。你可以看看下面的示例code。
TextBox.Text
property is of string
type. You may look at the following sample code.
int.TryParse
如果解析成功,假如果失败,这将返回true。
This will return true if the parsing is successful and false if it fails.
int value;
if(int.TryParse(textBox1.Text,out value))
{
//parsing successful
}
else
{
//parsing failed.
}
Convert.ToInt32
如果解析不成功,这可能会抛出异常。
This may throw an exception if the parsing is unsuccessful.
int value = Convert.ToInt32(textBox1.Text);
int.Parse
int value = int.Parse(textBox1.Text);
以后,你可以像你使用if语句值
。
if(value > 0)
{
}
else
{
}
这篇关于从文本框获取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!