wpf 我无法将 Textbox.Text 转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13379958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't convert Textbox.Text to int
提问by Oned Zair
int InvoerEieren = Convert.ToInt32(txtInvoerEieren.Text);
while txt.InvoerEieren.Text is an Textbox with a random number user can put (1 - 999).
而 txt.InvoerEieren.Text 是一个文本框,用户可以输入一个随机数(1 - 999)。
This is what I get when computer runs that line of code.
这就是我在计算机运行该行代码时得到的结果。
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
mscorlib.dll 中发生类型为“System.FormatException”的未处理异常
附加信息:输入字符串的格式不正确。
I also have tried using "int.Parse()" function.
我也尝试过使用“int.Parse()”函数。
edit:
编辑:
When I create a new function with this code:
当我使用此代码创建新函数时:
int eenGetal = Convert.ToInt32(txtInvoerEieren.Text);
MessageBox.Show(eenGetal.ToString());
it's working, that's weird... why isn't it working for other function?
它正在工作,这很奇怪......为什么它不适用于其他功能?
回答by ?yvind Br?then
Your problem is that what is inside the textbox can't be converted to an int. an int represent a whole number, and cannot have any decimal spaces.
您的问题是文本框内的内容无法转换为 int。int 代表一个整数,不能有任何小数位。
So you can use 1, 5 ,34 but not 1,23 or 4.54. In addition you cannot use numbers larger than +/-2,147,483,647.
所以你可以使用 1, 5 ,34 但不能使用 1,23 或 4.54。此外,您不能使用大于 +/-2,147,483,647 的数字。
if you need larger numbers, use longinstead ( and then long.Parse). If you need to have decimal points use decimal(and decimal.Parse). Note that if you are parsing decimals, you have to take the culture into consideration since you have to use the correct decimal marker according to the culture you are using with decimal.Parse.
如果您需要更大的数字,请long改用 ( 然后long.Parse)。如果您需要小数点,请使用decimal(和decimal.Parse)。请注意,如果您正在解析小数,则必须考虑文化,因为您必须根据与decimal.Parse.
And since it's user input you should consider using the TryParsevariants instead, since they won't "blow up" if the user gives an incorrect input (ie. Non-numeric).
由于它是用户输入,您应该考虑使用TryParse变体,因为如果用户输入不正确(即非数字),它们不会“爆炸”。
回答by Bob.
If you are trying to convert a string to Double, you should use the Double.TryParse Methodto parse the value because it wraps a try/catch around the conversion for you.
如果您尝试将字符串转换为 Double,您应该使用Double.TryParse 方法来解析该值,因为它为您包装了一个 try/catch 转换。
double decimalValue;
bool success = Double.TryParse(txtInvoerEieren.Text, out decimalValue);
if(success) { Debug.WriteLine("Successful parsing to Double!"); }
else { Debug.WriteLine("Parsing to Double failed!"); }
Similar for converting to Int32 values, you should use Int32.TryParse Method
类似于转换为 Int32 值,您应该使用Int32.TryParse 方法
int decimalValue;
bool success = Int32.TryParse(txtInvoerEieren.Text, out decimalValue);
if(success) { Debug.WriteLine("Successful parsing to Int32!"); }
else { Debug.WriteLine("Parsing to Int32 failed!"); }
回答by paparazzo
Put a Debug.WriteLine(txtInvoerEieren.Text);See if it is what you think it is.
把Debug.WriteLine(txtInvoerEieren.Text);看它是否是你认为它是。
It is a textbox.
If you don't assign a value then will be null or string.empty.
Both null and string.empty will fail that Int parse.
As in the comment use
它是一个文本框。
如果您不分配值,则将为 null 或 string.empty。
null 和 string.empty 都会使 Int 解析失败。
如在评论中使用
Int32.TryParse(txtInvoerEieren.Text, out InvoerEieren)
You can bind to Int?
But you will need to use a converter to convert string.empty to null.
And you will need to handle keydown and not allow anything but digits.
Handle keydown will not deal with sting.empty that has to be a converter.
Well this is the only way I was able to bind to an Int.
你可以绑定到Int吗?但是您需要使用转换器将 string.empty 转换为 null。
而且您将需要处理 keydown 并且不允许除数字之外的任何内容。处理 keydown 不会处理必须是转换器的 sting.empty。好吧,这是我能够绑定到 Int 的唯一方法。
回答by Olivier Jacot-Descombes
If you are not sure whether the user has typed in a text representing a correct number, I would suggest you to use another conversion method that checks the validity of the number
如果您不确定用户是否输入了代表正确数字的文本,我建议您使用另一种转换方法来检查数字的有效性
int InvoerEieren;
if (Int32.TryParse(txtInvoerEieren.Text, out InvoerEieren)) {
Console.WriteLine("The number is {0}", InvoerEieren);
} else {
Console.WriteLine("\"{0}\" cannot be converted to int!",
txtInvoerEieren.Text);
}
回答by Rohit Vats
Try Int32.TryParseinstead of Int32.Parse-
尝试Int32.TryParse代替Int32.Parse-
int InvoerEieren;
Int32.TryParse(txtInvoerEieren.Text, out InvoerEieren);
回答by Mehbube Arman
Unless there is some wrong input, this should never have happened. Although you can change this by adding a KeyPress event on this textbox. you can do this adding these line in your event handler function:
除非有一些错误的输入,否则这不应该发生。尽管您可以通过在此文本框上添加 KeyPress 事件来更改此设置。你可以在你的事件处理函数中添加这些行:
if (char.IsNumber(e.KeyChar))
{
e.Handled = false;
}
else
{
MessageBox.Show("Please Enter Number");
e.Handled = true;
}
I think this should solve your problem.
我认为这应该可以解决您的问题。
回答by datazbytes
You could try using the Int32.TryParse method. If the in value is invalid format, or just text it should be handling exceptions internally and returning you a zero value.
您可以尝试使用 Int32.TryParse 方法。如果输入值是无效格式,或者只是文本,它应该在内部处理异常并返回零值。

