gpt4 book ai didi

c# - 我无法制作消息框,有错误吗?

转载 作者:行者123 更新时间:2023-11-30 20:01:15 25 4
gpt4 key购买 nike

我正在尝试添加到我的汽车租赁计划中。抱歉有这些问题。我仍在学习 :)。因此,我希望我的表单显示一条错误消息,当您未输入数字或文本框为空白时,该消息会弹出。我试过:

 //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));

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

最佳答案

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

看拼写,让智识帮你掉进成功的坑里。

在这种情况下你根本不需要它。 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));

关于c# - 我无法制作消息框,有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com