Skip to content

Instantly share code, notes, and snippets.

@joebuschmann
Created December 18, 2014 20:49
Show Gist options
  • Select an option

  • Save joebuschmann/ca5b5147ef6b6391a73c to your computer and use it in GitHub Desktop.

Select an option

Save joebuschmann/ca5b5147ef6b6391a73c to your computer and use it in GitHub Desktop.
The wrong way to validate and close a modal form
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