我在C#中构建Windows窗体,并试图测试用户输入是否在1和7之间(表示天数在一周内,电影可以出租)。如果测试返回false,那么我想输出一条错误消息。我正在使用文本框来获取用户输入。问题是,我一直在运行程序时收到此错误:如何在C#中使用TextBox时测试Integer是否在范围内
System.FormatException了未处理
的HResult = -2146233033
消息=输入字符串的不正确的格式。
有人可以请告诉我我做错了什么。 预先感谢您。
这是我写的代码..
private void nightsRentedTextBox_TextChanged_1(object sender, EventArgs e)
{
Boolean inputBoolean = true;
if (int.Parse(nightsRentedTextBox.Text) < 1)
{
MessageBox.Show("Enter a number between 1 and 7", "INPUT ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error);
inputBoolean = false;
}
开始与使用的TryParse()..... –