Created
December 18, 2014 20:49
-
-
Save joebuschmann/ca5b5147ef6b6391a73c to your computer and use it in GitHub Desktop.
The wrong way to validate and close a modal form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public event EventHandler OkClick; // Eliminate custom event | |
| public bool Success { get; set; } // Eliminate custom flag | |
| private void btnOK_Click(object sender, EventArgs e) | |
| { | |
| if (Validate()) | |
| { | |
| _selectedUsers.Clear(); | |
| foreach (var user in _allUsers.Where(u => u.Selected)) | |
| _selectedUsers.Add(user.UserId, user.UserName); | |
| if (OkClick != null) | |
| OkClick(this, new EventArgs()); // Custom event is unnecessary | |
| // Eliminate the custom flag by using the DialogResult property | |
| this.Success = true; | |
| // Unnecessary when the button's DialogResult property is set to OK. | |
| // Form will automatically close. | |
| this.Hide(); | |
| } | |
| } | |
| // This method can be removed | |
| private void btnCancel_Click(object sender, EventArgs e) | |
| { | |
| // Eliminate the custom flag by using the DialogResult property. | |
| this.Success = false; | |
| // Unnecessary when the button's DialogResult property is set to Cancel. | |
| // Form will automatically close. | |
| this.Hide(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment