[c#] Check if TextBox is empty and return MessageBox?

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

This question is related to c# winforms textbox

The answer is


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
        }

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

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

...


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


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

Use something such as the following:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 

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

}

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


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;
    } 

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to textbox

Add two numbers and display result in textbox with Javascript How to check if a text field is empty or not in swift Setting cursor at the end of any text of a textbox Press enter in textbox to and execute button command javascript getting my textbox to display a variable How can I set focus on an element in an HTML form using JavaScript? jQuery textbox change event doesn't fire until textbox loses focus? PHP: get the value of TEXTBOX then pass it to a VARIABLE How to clear a textbox once a button is clicked in WPF? Get current cursor position in a textbox