Thursday, August 31, 2006

"Drop and Forget Dialog Buttons"

The Code Project - Drop and Forget Dialog Buttons

"If you are anything like me, you've been spoiled by Intellisense and want everything to go as quickly as possible when coding. So, when I make dialog boxes (as I am sure you do too), why should I have to set 4-5 things every time I want an OK or Cancel button on a form?

Whenever I drop a button on a form, I inevitably forget one of the following 4 steps (except #3, that one's kind of obvious).

  1. Set the Name to something resembling "OK" or "Cancel" so that it's not just called "button1".
  2. Set the DialogResult to DialogResult.OK or DialogResult.Cancel.
  3. Set the Text to "OK" or "Cancel".
  4. Set the Form's AcceptButton/CancelButton to your new OK/Cancel button (or pressing Enter/ESC won't press OK/Cancel).

I can't tell you how many times I have hit OK or Cancel and then wondered why it didn't do anything. Or the right thing. Or only half of the right thing--the half in the OK button's event handler but not the half in the form's if (dialog.ShowDialog() == DialogResult.OK) block. It seems like when it comes to OK and Cancel buttons, they should "just work".

Out of completeness, I have added "Yes" and "No" buttons as well, which allows us to do Yes/No and Yes/No/Cancel scenarios as well. I'll leave the useless Abort/Retry/Ignore scenario as a reader exercise.


I dig these kinds of simple time/brain cell saving/I hate re-inventing the wheel projects...

No comments: