0

I have two procedures in a form. One of them checks null fields and the second one inserts a record.

private void CheckNullValues()
{
    if (textBox1.Text == "") { 
        MessageBox.Show("Field [Email] can not be empty","Information",MessageBoxButtons.OK, MessageBoxIcon.Information);
        textBox1.Focus();
        return();
    }
}

private void buttonAdd_Click(object sender, EventArgs e)
{
     CheckNullValues();
     MessageBox.Show("Insert record");
}

How can I stop executing the action in CheckNullValues procedure if the textBox1 is empty and not let it show MessageBox.Show("Insert record")?

  • 2
    Change the return type of your `CheckNullValues` method to `bool`. You might want to change its name to something that reflects the returned value though. For example: `private bool ValidInputs() { // ... }` and then `if(!ValidInputs()) return;`. – 41686d6564 stands w. Palestine Jul 27 '18 at 01:34
  • You may want to consider a better solution for validations: [DataAnnotations Validation Attributes in Windows Forms](http://www.reza-aghaei.com/dataannotations-validation-attributes-in-windows-forms/) – Reza Aghaei Jul 27 '18 at 02:27
  • You can also validate, [using `Validating` event and show error summary](https://stackoverflow.com/a/33080822/3110834). – Reza Aghaei Jul 27 '18 at 02:28

3 Answers3

2

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;
}
  • i think, this approach causes spreading logic among methods. so if you, let's say, want to show error messages on form instead of showing messageboxes, you will have to rewrite validation method. i would suggest to use out-parameters for returning error messages from validation methods, and then do whatever you want with them. – Sam Sch Jul 27 '18 at 07:26
0

If the function checking if something is null is set to return true or false you can then use an if statement in your click event.

if (!checkForNulls()) {
    MessageBox.Show("Insert record");
}
G. LC
  • 794
  • 1
  • 8
  • 27
0

Please modify your code as below: private bool CheckNullValues() {

        if (textBox1.Text == "")
        {
            MessageBox.Show("Field [Email] can not be empty", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox1.Focus();
            return false;
        }
        else
        {
            return true;
        }
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        if (CheckNullValues())
        {
            MessageBox.Show("Insert record");
        }       
    }
Ruhul Amin
  • 87
  • 6