Check if TextBox is empty and return MessageBox?
Asked Answered
C

8

23

I made this statement to check if TextBox is empty, but the MessageBox always shows up wether the TextBox is empty or not.

    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";
            }*/
Cloy answered 27/5, 2011 at 18:45 Comment(2)
Please, fix the code sample. Pay attention to opening and closing bracesEliason
@sam: Pls accept answers that work for you.Imperfection
E
65

Try this condition instead:

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

This will take care of some strings that only contain whitespace characters and you won't have to deal with string equality which can sometimes be tricky

Eliason answered 27/5, 2011 at 18:49 Comment(7)
@ghost_j1: please, provide some context. Maybe extend your code sample to include the whole methodEliason
@ghost_j1: it's obviously not the whole code (it doesn't have the closing brace), but it's already obvious what the problem is, see my another answer. You clear the textbox before you validate itEliason
MOG... sooooooo stupid mistake made by me :S :S that's what happen when you programing all the day hehehe i need breakCloy
@ghost_j1: no problem, it happens. My advice is to break your code into smaller methods to make it easier to reason about.Eliason
@ghost_j1: also, feel free to accept an answer you found most useful if your problem is solved. It helps people to concentrate on other questions which still need new ideasEliason
thanks for advice man but I am beginner in programing and that's my first program... by the way after this message show up the datagridview insert new row so is there is a way to remove it or maybe discard the whole progress ???Cloy
@ghost_j1: you want to validate all fields first and only perform insert\update logic if everything looks alright. So I suggest you move the code that checks whether MaterialTextBox.Text is empty in the beginning of the method. If it is empty, show a message box and just exit from the method using return statement. That way no code below the check will be executedEliason
E
12

Well, you are clearing the textbox right before you check if it's empty

/* !! 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();
}
Eliason answered 27/5, 2011 at 19:12 Comment(1)
yea thanks i noticed this stupid mistake done by me...but after this message show up the datagridview insert new row so is there is a way to remove it or maybe discard the whole inserted row or progress ???Cloy
P
3

Use something such as the following:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
Predictory answered 27/5, 2011 at 18:47 Comment(1)
This will not handle the whitespacesSuu
B
2

Try doing the following

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

Hope it helps

Boyette answered 28/11, 2016 at 5:44 Comment(0)
S
0

Adding on to what @tjg184 said, you could do something like...

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

...

Singspiel answered 27/5, 2011 at 18:59 Comment(0)
M
0
if (MaterialTextBox.Text.length==0)
{
message

}
Micrometer answered 30/7, 2013 at 9:44 Comment(0)
E
0

For multiple text boxes - add them into a list and show all errors into 1 messagebox.

// 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;
    } 
Elongate answered 15/5, 2020 at 12:27 Comment(0)
T
0

Becasue is a TextBox already initialized would be better to control if there is something in there outside the empty string (which is no null or empty string I am afraid). What I did is just check is there is something different than "", if so do the thing:

 if (TextBox.Text != "") //Something different than ""?
        {
            //Do your stuff
        }
 else
        {
            //Do NOT do your stuff
        }
Tymothy answered 22/7, 2020 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.