Title: Validating before invoking ShowDialog
Tags: c#;winforms;validation;showdialog
Question: I'm working with two forms where I have to validate inputs in the first form before I can switch to the 2nd form.
My Code in the first form is as follows:
private void btnSignIn_Click(object sender, EventArgs e)
GetInputs();
IsValidata();
de.Text = string.Format("Hello! {0} {1} {2}{3} {4} {5})",
Fname, Lname, "(U", Unum1, "-", Unum2);
this.Hide();
de.ShowDialog();
IsValidata() is a series of validations where it checks if the input is present and if it is a string.
I tried putting these inside the curly braces however validation would not work:
de.Text = string.Format("Hello! {0} {1} {2}{3} {4} {5})",
Fname, Lname, "(U", Unum1, "-", Unum2);
this.Hide();
de.ShowDialog();
The only way I'm able to load form2 is by taking out the curly braces but the logic is not correct and it's not exactly validating my inputs. What am I missing?
Comment: I've tried putting if (IsValidData()) and within it is the de.Text = ....de.ShowDialog() but when I did that, the button to display the 2nd form would not work. It simply stays in form1.

Comment: I'm trying to have a returned MessageBox asking the user to correct the error. Say for instance, if there's no input, it should prompt the use to input a required value. If the user inputs a digit rather than a string, it will prompt the user to input only characters.

Comment: Hi, can you please show the code for the `IsValidata()` method?  If we assume that this returns a `bool`, indicating whether or not we are "valid", you currently don't do anything with that.  Likely missing an `if` that only shows the second form if `IsValidata()` returns `true`.

Comment: But that's what you want, right?  If the data is not valid, then don't show form 2.  What type of behaviour are you actually wanting?

Here is the accepted answer: You are missing some control flow logic in your event handler.
Let's assume that you want to simply show a message box to the user if the input is invalid.  Otherwise, if the input is valid, display the second form.
private void btnSignIn_Click(object sender, EventArgs e)
GetInputs();
if (IsValidata())
de.Text = string.Format("Hello! {0} {1} {2}{3} {4} {5})",
Fname, Lname, "(U", Unum1, "-", Unum2);
this.Hide();
de.ShowDialog();
else
{
// Show a message box, with an OK button only
MessageBox.Show("Invalid Input", "Invalid Input", MessageBoxButtons.OK);
In the example above, we:
Get the input
Check if the data is valid
If the data is valid, show the new form
If the data is not valid, we display a message to the user
If you want to display a message based on the results of the validation (i.e. if the user input digits only instead of a string), you'll need to add that logic in, and construct the message that you want to display.
Update:
Ok, so you actually want the validation routine to return the type of validation that has failed (if any fail).
So let's define a new type that the validation method can return:
public class ValidationResult
public bool IsValid { get; set; }
public string UserMessage { get; set; }
Now we can update the validation method:
private ValidationResult IsValidata()
//validate the First Name textbox
if (!IsPresent(txtFirst, "First Name"))
return new ValidationResult {
IsValid = false,
UserMessage = "Please enter a First Name"
};
// rest of valiation rules here,
// setting the UserMessage as appropriate
// If no validation checks fail, then we are valid
return new ValidationResult { IsValid = true };
Now your event handler can use the results:
private void btnSignIn_Click(object sender, EventArgs e)
GetInputs();
var validationResult = IsValidata();
if (validationResult.IsValid)
de.Text = string.Format("Hello! {0} {1} {2}{3} {4} {5})",
Fname, Lname, "(U", Unum1, "-", Unum2);
this.Hide();
de.ShowDialog();
else
{
// Show a message box, with an OK button only
MessageBox.Show(validationResult.UserMessage, "Invalid Input", MessageBoxButtons.OK);
Comment for this answer: I just want to be sure I understand this. I have created separate methods for each bool condition in IsValidData() with its respective message if the error if invoked. Is this what you meant or would I have to add a separate message within the method where login values are inputted and validated?
Comment for this answer: Thank you so much. I understand it now. I've been attempting to implement the validation method as you have show however, looking at your updated response, I see I'm missing the accessor and an instance of a validation result.
Comment for this answer: The answer has been updated to cover the additional requirements that you have.  Note that the question wasn't very clear as to what you wanted to achieve.  It's always a good idea to be as thorough as you can with questions here.

Here is another answer: This is the code for my Valid data:
private bool IsValidata()
//validate the First Name textbox
if (!IsPresent(txtFirst, "First Name"))
{
return false;
}
if (!StringValidator(Fname))
{
return false;
}
//validate the Last Name textbox
if (!IsPresent(txtLast, "Last Name"))
{
return false;
}
if (!StringValidator(Lname))
{
return false;
}
return true;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:09/25/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><winforms><validation><showdialog>', 'Title': 'Validating before invoking ShowDialog', 'ViewCount': 131, 'id': 52489232, 'user_AboutMe': '', 'user_AccountId': 9772184.0, 'user_DisplayName': 'Pinkfit', 'user_Reputation': 17.0, 'user_Views': 31.0, 'user_id': 7243644.0}}