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);
开发者_如何学Go 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";
}*/
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))
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
Adding on to what @tjg184 said, you could do something like...
if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim()))
...
if (MaterialTextBox.Text.length==0)
{
message
}
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;
}
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
}
精彩评论