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