How to cancel Button click action when required fields are empty?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to cancel Button click action when required fields are empty?



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")?


CheckNullValues


MessageBox.Show("Insert record")





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;.
– Ahmed Abdelhameed
49 mins ago




CheckNullValues


bool


private bool ValidInputs() { // ... }


if(!ValidInputs()) return;




3 Answers
3



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.


CheckNullValues


bool



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:


ValidInputs()


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:


ValidInputs()


private bool ValidInputs()
{
if (TextBoxEmpty(textBox1, "Field [Email] can not be empty")) return false;
if (TextBoxEmpty(textBox2, "Some other message")) return false;
// ...

return true;
}



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



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty