检查文本框是否为空并返回消息框?

23

我使用了这段代码来检查 TextBox 是否为空,但是无论 TextBox 是否为空,MessageBox 都会弹出。

    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";
            }*/

2
请修复代码示例。注意大括号的开闭。 - Dyppl
@sam:请接受对你有用的答案。 - D J
8个回答

65

请尝试使用这个条件:

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

这将处理一些仅包含空格字符的字符串,您将不必处理字符串相等性问题,有时可能会很棘手


@ghost_j1:请提供一些上下文。也许可以扩展您的代码示例以包括整个方法。 - Dyppl
@ghost_j1:显然这不是整个代码(它没有闭合括号),但问题已经很明显了,可以看看我的另一个答案。在验证之前,请先清空文本框。 - Dyppl
MOG...我犯了一个超级愚蠢的错误:S :S 这就是当你整天编程时会发生的事情,呵呵呵 我需要休息一下。 - sam
@ghost_j1:另外,如果您的问题已经解决,请随意接受您认为最有用的答案。这有助于其他人集中精力解决仍需要新思路的问题。 - Dyppl
@ghost_j1:你需要先验证所有字段,只有在一切看起来都没问题的情况下才执行插入\更新逻辑。因此,我建议你将检查 MaterialTextBox.Text 是否为空的代码移到方法的开头。如果它为空,显示一个消息框并使用 return 语句退出方法。这样,在检查以下代码之前,不会执行任何代码。 - Dyppl
显示剩余2条评论

12

你在检查文本框是否为空之前就清空了它

/* !! 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会插入新行,所以有没有办法将其删除或者放弃整个插入的行或进程? - sam

3

可以使用以下内容:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 

1
这将不处理空格。 - abhilash

2

尝试按照以下步骤进行:

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

希望能对您有所帮助。

0

继@tjg184所说的之后,你可以这样做...

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

...


0
if (MaterialTextBox.Text.length==0)
{
message

}

0

因为文本框已经初始化,最好控制一下是否有除空字符串(不是null或空字符串,我担心)以外的内容。我的做法只是检查是否有与“”不同的东西,如果有,就执行相应操作:

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

0
对于多个文本框 - 将它们添加到列表中,并将所有错误显示在一个消息框中。
// 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;
    } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接