【问题标题】:How to mark TextBox as empty? [duplicate]如何将文本框标记为空? [复制]
【发布时间】:2020-05-26 09:35:07
【问题描述】:

当没有在任何文本框中输入文本时,我希望打开一个新表单,其中包含一条消息

if (rectangleBox.Checked == true && (int.Parse(side_a.Text) > 0 && 
    int.Parse(side_b.Text) > 0))
    {
      var drawRectangle = new Rectangles(70 + int.Parse(side_a.Text), 
      80 + int.Parse(side_b.Text),
      50 + int.Parse(side_a.Text), 70 + int.Parse(side_b.Text));
      Figures.Add(drawRectangle);
    };  

if (string.IsNullOrWhiteSpace(side_a.Text) &&
    string.IsNullOrWhiteSpace(side_b.Text) &&
    string.IsNullOrWhiteSpace(side_c.Text))
    {
        Form1 form1 = new Form1();
        Form1.ShowDialog();
    }

但我在以下几行中遇到错误

    if (rectangleBox.Checked == true && (int.Parse(side_a.Text) > 0 && 
        int.Parse(side_b.Text) > 0))            

"System.FormatException: '输入字符串的格式不正确。'"

【问题讨论】:

  • 这能回答你的问题吗? Check Whether a TextBox is empty or not 完全相同的副本
  • “标记为空”是什么意思??
  • @Taw 当 TextBoxes 中没有文本时
  • 人们试图帮助你,但你真的很难! “标记”是什么意思???另外:“没有文字”或没有有效数字“?一个或所有盒子中也缺少数据? - 一般规则,除非你不能清楚地表达你的目标,否则你不能为它编写代码..

标签: c# .net winforms textbox


【解决方案1】:

您可以使用string.IsNullOrEmpty 或string.IsNullOrWhiteSpace 函数来检查TextBox 是否为空。

if(string.IsNullOrEmpty(TextBox1.Text))
{
  // do something
}

【讨论】:

  • 或 IsNullOrWhiteSpace
  • @JonasH 你是对的。
  • 如何回答这个问题?他问的不是测试而是标记..
  • @TAW 为什么答案没有回答问题?你理解的问题是什么?
  • 正如我上面写的,我不明白这个问题。不过,也许你猜对了..
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 2019-03-06
  • 2017-06-15
  • 2017-08-17
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多