After I posted the code for DialogBox and PopupBox I got a few interesting questions on the CodePlex message boards. One key request was the ability for the DialogBox to remain active if certain validation conditions were not met. This made sense.
I went back and examined how my code worked and how the WindowWorkspace class works. I discovered that I was wiring up some events for no reason at all. It turns out that the WindowWorkspace understands how to close itself by default as long as you have a button on the UserControl that has a DialogResult set to something other than DialogResult.None. My guess is this behavior has less to do with the WindowWorkspace and more to do with default Window Forms behavior,l which is what the WindowWorkspace utilizes to show your faux Dialog/UserControl.
With that knowledge I was able to eliminate the events and simplify the DialogBox base class. A couple other changes that are more noticable:
- The base DialogBox class is no longer marked abstract. It turns out that if you mark a class as abstract, the designer in Visual Studio cannot display any subclassed objects of that type. And since the most convenient way to design dialogs is with the visual designer I eliminated the abstract declaration.
- Changed InitDialog() to OnShow(). This method is still overridable and it is the method you should override if you want to handle any dialog initialization prior to displaying the dialog. The WindowWorkspace changes I’ve made call this method when it shows the DialogBox. This was more semantics than anything, but it made more sense to me.
So how do these changes affect the code? The big difference now is that must wire-up the click handler for your dialog buttons and set the DialogResult on the Control. This is not necessarily a bad thing, however, because now it gives you the chance to perform validation prior to setting the DialogResult. An example in the sample code is a Logon dialog. The LogonDialog has one button on it. Handling the Click event looks like this:
private void _logonButton_Click(object sender, System.EventArgs e)
{
if (_usernameTextBox.Text == "Admin" && _passwordTextBox.Text == "password")
{
DialogResult = DialogResult.OK;
}
else
{
DialogResult = DialogResult.None;
}
}
A DialogResult of None will cause the Dialog to not close. Any other DialogResult will close the dialog. Then it’s up to you to check the DialogResult in code to make sure you got the result you want.
Once again, I hope people find this useful.
Dennis Childers says:
I have downloaded your CABSample_v2 code and am really interested in using the windowworkspace as a dialog from which I can user inpu for my application. My problem at this point is that you speak of you DialogBox object having a method called InitializeClickHandlers but I cannot seem to find this method. Could you help.
Thanks, DC
March 1, 2008, 12:51 pm