Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";
            }*/
like image 745
sam Avatar asked May 27 '11 18:05

sam


People also ask

How do I check if a TextBox is empty?

IsNullOrEmpty() function has a boolean return type and returns true if the string is either null or empty and otherwise returns false . We can use the String. IsNullOrEmpty() function on the string inside the TextBox. Text property to check whether the text inside the text box is empty or not.

How check multiple TextBox is empty or not in C#?

if (string. IsNullOrWhitespace(tbAddress. text)) { XtraMessageBox. Show("An address is required", "Error"); tbAddress.

How check TextBox is empty in VB net?

First declare emptyTextBoxes as the selection of any text property with length 0. then check if there is any textbox with length = 0 and show it. Its the same exact answer; nothing is added to tailor it to the user's needs and you did not even upvote Tim's answer which you obviously found helpful.

How do you check if all textboxes are filled C#?

GetType() == typeof(TextBox)) if (!( String. IsNullOrEmpty(control.


3 Answers

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();
}
like image 121
Dyppl Avatar answered Sep 30 '22 05:09

Dyppl


Use something such as the following:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
like image 35
tjg184 Avatar answered Oct 01 '22 05:10

tjg184


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

like image 29
cell-in Avatar answered Oct 01 '22 05:10

cell-in