我不能发消息框,有错误吗?

Gun*_*nar 2 c#

我想加入我的租车计划.抱歉有这些问题.我仍在学习 :).所以我希望我的表单显示一个错误消息,当您不输入数字或文本框为空时弹出.我试过了:

 //If nothing is inserted in text, error box.
        int value = 0;
        if (string.IsNullOrWhitespace(txtBegin.Text) || !int.TryParse(txtBegin.Text, out value)) // Test for null or empty string or string is not a number
            MessageBox.Show("Please enter a number!");
        else
            MessageBox.Show(string.Format("You entered: {0}!", value));
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误:'string'不包含'IsNullOrWhitespace'的定义.谁能帮我?

Han*_*ant 7

使用String.IsNullOrWhiteSpace()需要以.NET 4.0或更高版本为目标.项目+属性,应用程序选项卡,目标框架设置.需要VS2010或更高.

观看拼写,让IntelliSense帮助您陷入成功之中.

在这种情况下,你根本不需要它.TextBox的Text属性永远不能为null,如果字符串为空,TryParse()将返回false.固定:

    int value = 0;
    if (!int.TryParse(txtBegin.Text, out value))
         MessageBox.Show("Please enter a number!");
    else MessageBox.Show(string.Format("You entered: {0}!", value));
Run Code Online (Sandbox Code Playgroud)