检查TextBox是否为空并返回MessageBox?

时间:2011-05-27 18:45:48

标签: c# winforms textbox

我做了这个语句来检查TextBox是否为空,但MessageBox总是显示出来 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";
            }*/

8 个答案:

答案 0 :(得分:53)

请尝试此条件:

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

这会处理一些只包含空白字符的字符串,你不必处理有时候很棘手的字符串相等

答案 1 :(得分:11)

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

/* !! 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();
}

答案 2 :(得分:2)

使用以下内容:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 

答案 3 :(得分:2)

尝试执行以下操作

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

希望有所帮助

答案 4 :(得分:0)

再加上@ tjg184所说的,你可以做点像......

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

...

答案 5 :(得分:0)

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

}

答案 6 :(得分:0)

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

答案 7 :(得分:0)

因为是已初始化的TextBox,所以最好控制空字符串(恐怕不是空值或空字符串)之外是否有东西。我所做的只是检查是否有与“”不同的东西,如果是这样的话:

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