[英]I'm trying to make a random number generator for my game and assign a integer to it so i can use it later but give me a error
[英]I'm trying to make it so that the number cant be increased in a TextBox
我是一个试图让用户无法从 .mdf 文件增加文本框中的数字的菜鸟,但是当我测试运行并测试程序的这一部分时它崩溃了,我看到“系统.FormatException:'输入字符串的格式不正确。'”错误消息。
private void HoeveelheidTextBox_TextChanged(object sender, EventArgs e)
{
if (42 < int.Parse(hoeveelheidTextBox.Text))
{
MessageBox.Show("je kan niet schoenen erbij doen");
{
hoeveelheidTextBox.ToString();
hoeveelheidTextBox.Text = "";
}
}
}
(je kan niet schoenen erbij doen = 你不能再添加鞋子的 hoeveelheid = 数量)
正如您在代码中看到的,我尝试将 TextBox 转换回字符串,但是,我仍然收到错误消息。
我想要的结果是,当数字大于最初的数字时,程序会清除 TextBox。
好吧,似乎HoeveelheidTextBox
的值可能不是有效的int
值(例如,空字符串); 让我们使用TryParse
而不是Parse
:
private void HoeveelheidTextBox_TextChanged(object sender, EventArgs e) {
if (int.TryParse(hoeveelheidTextBox.Text, out int value)) {
// TryParse succeeded; hoeveelheidTextBox.Text has an integer value
// Let's check what the value is
if (42 < value) {
// hoeveelheidTextBox.Text has an integer value that exceeds 42
MessageBox.Show("je kan niet schoenen erbij doen");
hoeveelheidTextBox.Text = "";
}
else {
// hoeveelheidTextBox.Text has an integer value which is 42 or below it
}
}
else { // TryParse failed;
// hoeveelheidTextBox.Text doesn't have an integer value
// (it can be an empty string, "bla-bla-bla" etc.)
hoeveelheidTextBox.Text = "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.