Introduction
Vista's new TaskDialog API’s
are really cool and should see lame message boxes banished from you application. We've already seen
some articles on using them from C++ but they are not trivial to use from managed code, so here is
some code that makes using all the features of the new Task Dialogs really easy in WinForms.
The code is C# and is StyleCop, FXCop and PreSharp clean. Usability and discoverability were given
preference to performance and size, with plenty of comments that should be really useful in Intellisense
as you explore what Task Dialogs can do. You don’t have to use anything with a shouted (all capitals)
name and all the features of Task Dialog are exposed including the callback and actions you can
perform on an active Task Dialog. If you hate the very idea of wrappers and want the raw interop
declarations, they are all there too.
Using the code
The main class is TaskDialog
. The simplest usage gets you MessageBox behavior:
TaskDialog taskDialog = new TaskDialog();
taskDialog.WindowTitle = "My Application";
taskDialog.MainInstruction = "Do you want to do this?";
taskDialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
int result = taskDialog.Show();
if (result == (int)DialogResult.Yes)
{
}
From there you can add hyperlinks for help, more structure (adding content, extended info and footer to
main instruction), a checkbox for "don’t ask me again", custom button names, etc.:
private void SampleUsageComplex()
{
TaskDialog taskDialog = new TaskDialog();
taskDialog.WindowTitle = "My Application";
taskDialog.MainInstruction = "Do you want to do this?";
taskDialog.EnableHyperlinks = true;
taskDialog.Content = "If you do this there could be all sorts of consequences. " +
"If you don't there will be other consequences. " +
"You can <A HREF=\"Learn\">learn more about those consequences </A> or more " +
"about <A HREF=\"blah\">blah blah blah</A>.";
taskDialog.Callback = new TaskDialogCallback(this.MyTaskDialogCallback);
taskDialog.VerificationText = "Don't ask me this ever again.";
taskDialog.VerificationFlagChecked = false;
TaskDialogButton doItButton = new TaskDialogButton();
doItButton.ButtonId = 101;
doItButton.ButtonText = "Do It";
TaskDialogButton dontDoItButton = new TaskDialogButton();
dontDoItButton.ButtonId = 102;
dontDoItButton.ButtonText = "Don't Do It";
taskDialog.Buttons = new TaskDialogButton[] { doItButton, dontDoItButton };
bool dontShowAgain;
int result = taskDialog.Show(null, out dontShowAgain);
if (dontShowAgain)
{
}
if (result == doItButton.ButtonId)
{
}
}
private bool MyTaskDialogCallback(
ActiveTaskDialog taskDialog,
TaskDialogNotificationArgs args,
object callbackData)
{
if (args.Notification == TaskDialogNotification.HyperlinkClicked)
{
if (args.Hyperlink.Equals("Learn", StringComparison.Ordinal))
{
}
else if (args.Hyperlink.Equals("blah", StringComparison.Ordinal))
{
}
}
return false;
}
To get a better idea of all the things you can do with a TaskDialog
download the demo, extract the TaskDialogTest.exe
and TaskDialog.dll to the same directory and run TaskDialogTest.exe. You'll get a rather large dialog which,
while not beautiful, will let you try out the TaskDialog and perhaps sample what different uses in your
application would look like.
To make use of TaskDialog
in your own solution, download
the source and include the following source files in your project and adapt them as you see fit:
- ActiveTaskDialog.cs
- TaskDialog.cs
- TaskDialogNotificationArgs.cs
- UnsafeNativeMethods.cs
Alternatively you could include the TaskDialog project into you solution.
I look forward to seeing a lot fewer Message Boxes in you applications and I hope this makes it easier.
Keep in mind good UI design when you're tempted to use all those features in the one dialog.