Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save joebuschmann/a8da72fcdaaea1847b9c to your computer and use it in GitHub Desktop.
The wrong way to display a WinForms modal dialog
private void btnShowUserDialog_Click(object sender, EventArgs e)
{
var form = new SelectUserForm(); // Wrap in using block
form.SelectedUsers = SelectedUsers;
// Don't use a custom event.
// Instead get the selected users after the ShowDialog() method returns.
form.OkClick += SelectUserForm_OnOkClick;
form.ShowDialog(this);
// Use the built-in DialogResult property instead of
// creating a custom property.
if (form.Success)
{
lblUsers.Text = SelectedUsers.Values.ToDelimitedString(", ");
lblUsers.Tag = SelectedUsers.Keys.ToDelimitedString(",");
}
// Missing call to form.Dispose()
}
private void SelectUserForm_OnOkClick(object sender, EventArgs e)
{
// Creating a custom event to process results is unnecessary
SelectedUsers = ((SelectUserForm)sender).SelectedUsers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment