Reputation: 133
I am new to C#
I am trying to validate a form using C#. I want a MessageBox
to show when validation failed. I have the error:
"No overload method 'UserFormValidation '"
The code is below
// Validation Method
public void UserFormValidation(object sender, CancelEventArgs e)
{
if (textSurname.Text == " ")
{
MessageBox.Show("Please insert your Surname");
}
else if (textFirstname.Text == " ")
{
MessageBox.Show("Please insert your Firstname");
}
}
private void UserRegistrationSend_Click(object sender, EventArgs e)
{
// Call the validation Method
UserFormValidation();
// if validation pass print message below
MessageBox.Show("User Registed", "User Message");
}
Upvotes: 0
Views: 114
Reputation: 403
Just call
UserFormValidation(sender, new CancelEventArgs());
If you want to validate the form controls you could do it with ValidateChildren() Method.
Please take a look at the msdn sample code:
[1]: https://msdn.microsoft.com/en-us/library/ms158374(v=vs.110).aspx
The other problem is
if (textSurname.Text == " ")
This condition is True, if you have exactly 1 whitespace.
You have to do it like that way:
if (IsNullOrWhiteSpace(textSurname.Text))
{...}
Upvotes: -1
Reputation: 853
It is because your method UserFormValidatin needs 2 parameters, and you call it without parameters
Upvotes: 0
Reputation: 35308
You have defined your UserFormValidation
method with parameters:
public void UserFormValidation(object sender, CancelEventArgs e)
But you're calling it without parameters:
UserFormValidation();
One way to fix this is to change the method signature so it takes no parameters as long as you don't really need them -- which it looks like from your present code. Another way is to simply give it parameters when you're calling it:
UserFormValidation(this, new CancelEventArgs());
A third option would be to add an overload that takes no parameters, thus keeping the one that does intact in case you're using it elsewhere. Inside your overload, you could simply call the one that takes params:
public void UserFormValidation()
{
UserFormValidation(this, new CancelEventArgs());
}
Which one you choose ultimately depends on how you're using it.
Upvotes: 3