【问题标题】:Check if TextBox is empty and return MessageBox?检查TextBox是否为空并返回MessageBox?
【发布时间】:2011-05-27 18:45:48
【问题描述】:

我做了这个语句来检查 TextBox 是否为空,但 M​​essageBox 总是出现 TextBox 是否为空。

    private void NextButton_Click(object sender, EventArgs e)
    {
        decimal MarkPoints, x, y;
        x = HoursNumericUpDown.Value;
        y = MarkNumericUpDown.Value;
        MarkPoints = x * y;

        //decimal MarkPoints = (decimal)HoursNumericUpDown.Value * (decimal)HoursNumericUpDown.Value;


        DataGridViewRow dgvRow = new DataGridViewRow();
        DataGridViewTextBoxCell dgvCell =  new DataGridViewTextBoxCell();

        dgvCell = new DataGridViewTextBoxCell();
        dgvCell.Value = MaterialTextBox.Text;
        dgvRow.Cells.Add(dgvCell);

        dgvCell = new DataGridViewTextBoxCell();
        dgvCell.Value = HoursNumericUpDown.Value;
        dgvRow.Cells.Add(dgvCell);

        dgvCell = new DataGridViewTextBoxCell();
        dgvCell.Value = MarkNumericUpDown.Value;
        dgvRow.Cells.Add(dgvCell);

        dgvCell = new DataGridViewTextBoxCell();
        dgvCell.Value = MarkPoints;
        dgvRow.Cells.Add(dgvCell);

        dataGridView1.Rows.Add(dgvRow);

        MaterialTextBox.Clear();
        HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
        MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;

        if (String.IsNullOrEmpty(MaterialTextBox.Text))
        {
            MessageBox.Show("Enter Material Name Please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //dataGridView1.Rows.Clear();
        }
        else
        {
            /*if (MarkNumericUpDown.Value < 50)
            {
                int index = dataGridView1.Rows.Add();
                dataGridView1.Rows[1].Cells[4].Value = "F";
            }
            else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
            {
                dataGridView1.Rows[index].Cells[4].Value = "F";
            }*/

【问题讨论】:

  • 请修复代码示例。注意大括号的开合
  • @sam:请接受适合您的答案。

标签: c# winforms textbox


【解决方案1】:

试试这个条件:

if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) {
    // Message box
}

这将处理一些只包含空白字符的字符串,您不必处理有时可能很棘手的字符串相等性

【讨论】:

  • @ghost_j1:请提供一些上下文。也许扩展您的代码示例以包含整个方法
  • @ghost_j1:这显然不是整个代码(它没有右括号),但问题出在哪里已经很明显了,请参阅我的另一个答案。在验证之前清除文本框
  • MOG...我犯的愚蠢的错误 :S :S 这就是你整天编程时发生的事情,呵呵,我需要休息
  • @ghost_j1:没问题,它发生了。我的建议是将您的代码分解为更小的方法,以便于推理。
  • @ghost_j1:另外,如果您的问题得到解决,请随时接受您认为最有用的答案。它可以帮助人们专注于仍然需要新想法的其他问题
【解决方案2】:

好吧,在检查文本框是否为空之前,您正在清除文本框

/* !! This clears the textbox BEFORE you check if it's empty */
MaterialTextBox.Clear();

HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;

if (String.IsNullOrEmpty(MaterialTextBox.Text))
{
        MessageBox.Show("Enter Material Name Please.", "Error", MessageBoxButtons.OK,    MessageBoxIcon.Warning);
            //dataGridView1.Rows.Clear();
}

【讨论】:

  • 是的,谢谢我注意到我犯的这个愚蠢的错误......但是在这条消息显示后 datagridview 插入新行所以有没有办法删除它或者可能丢弃整个插入的行或进度???
【解决方案3】:

使用如下内容:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 

【讨论】:

  • 这不会处理空格
【解决方案4】:

尝试执行以下操作

if (String.IsNullOrEmpty(MaterialTextBox.Text) || String.IsNullOrWhiteSpace(MaterialTextBox.Text))
    {
        //do job
    }
    else
    {
        MessageBox.Show("Please enter correct path");
    }

希望对你有帮助

【讨论】:

    【解决方案5】:

    除了@tjg184 所说的之外,您还可以执行类似...

    if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim())) 
    

    ...

    【讨论】:

      【解决方案6】:
      if (MaterialTextBox.Text.length==0)
      {
      message
      
      }
      

      【讨论】:

        【解决方案7】:

        对于多个文本框 - 将它们添加到列表中并将所有错误显示到 1 个消息框中。

        // Append errors into 1 Message Box      
        
         List<string> errors = new List<string>();   
        
         if (string.IsNullOrEmpty(textBox1.Text))
            {
                errors.Add("User");
            }
        
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                errors.Add("Document Ref Code");
            }
        
            if (errors.Count > 0)
            {
                errors.Insert(0, "The following fields are empty:");
                string message = string.Join(Environment.NewLine, errors);
                MessageBox.Show(message, "errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            } 
        

        【讨论】:

          【解决方案8】:

          因为是一个已经初始化的 TextBox 会更好地控制空字符串之外是否有东西(我担心这不是空字符串或空字符串)。我所做的只是检查是否有与“”不同的内容,如果有,请执行以下操作:

           if (TextBox.Text != "") //Something different than ""?
                  {
                      //Do your stuff
                  }
           else
                  {
                      //Do NOT do your stuff
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-02-03
            • 2017-10-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-11-24
            • 2011-08-22
            相关资源
            最近更新 更多