You need to change the type of your CheckNullValues
method to bool
in order for it to return a true or false value depending on whether the TextBox(es) is/are empty or not.
You might also want to change its name to something that reflects the returned value. I usually use something like this:
private bool ValidInputs()
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Field [Email] can not be empty","Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return false;
}
if (string.IsNullOrEmpty(textBox2.Text))
{
// ...
return false;
}
return true;
}
Then in your button click event, you can easily do something like:
if (!ValidInputs()) return;
Moreover, to avoid repeating code in the ValidInputs()
method, you can move the logic for validating the TextBox contents to separate method:
public bool TextBoxEmpty(TextBox txtBox, string displayMsg)
{
if (string.IsNullOrEmpty(txtBox.Text))
{
MessageBox.Show(displayMsg, "Required field",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtBox.Focus();
return true;
}
return false;
}
That way, your ValidInputs()
method becomes:
private bool ValidInputs()
{
if (TextBoxEmpty(textBox1, "Field [Email] can not be empty")) return false;
if (TextBoxEmpty(textBox2, "Some other message")) return false;
// ...
return true;
}