I have a WinForms application where, when all fields are entered, there is a save button.
On click of the save button, a messagebox appears stating 'record saved successfully'. The messagebox has two buttons, 'yes' and 'no'.
If yes, then the record should be saved and all the fields on the form should be cleared. If no is clicked, then all the fields should be cleared on the form without the record getting saved.
How can I implement this?
You don't need an event handler; the Show method of the MessageBox class returns a DialogResult:
DialogResult result = MessageBox.Show("text", "caption", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes){
//yes...
}
else if(result == DialogResult.No){
//no...
}
There is DialogResult
-enum to handle such things (from MSDN)
private void validateUserEntry5()
{
// Checks the value of the text.
if(serverName.Text.Length == 0)
{
// Initializes the variables to pass to the MessageBox.Show method.
string message = "You did not enter a server name. Cancel this operation?";
string caption = "No Server Name Specified";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(this, message, caption, buttons);
if(result == DialogResult.Yes)
{
// Closes the parent form.
this.Close();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With