Created
December 18, 2014 20:35
-
-
Save joebuschmann/a8da72fcdaaea1847b9c to your computer and use it in GitHub Desktop.
The wrong way to display a WinForms modal dialog
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
| 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