Click here to Skip to main content
13,260,181 members (54,025 online)
Click here to Skip to main content
Add your own
alternative version


100 bookmarked
Posted 5 Jan 2007

TaskDialog for WinForms

, 5 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Using Vista Task Dialog from WinForms - here is some code to do it.


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)
    // Do it.

TaskDialog with Yes No buttons

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)
        // Suppress future asks.
    if (result == doItButton.ButtonId)
        // Do it.

private bool MyTaskDialogCallback(
    ActiveTaskDialog taskDialog,
    TaskDialogNotificationArgs args,
    object callbackData)
    if (args.Notification == TaskDialogNotification.HyperlinkClicked)
        if (args.Hyperlink.Equals("Learn", StringComparison.Ordinal))
            // Show a help topic.
        else if (args.Hyperlink.Equals("blah", StringComparison.Ordinal))
            // Show a different help topic.
    return false;

TaskDialog with hyperlink and verify checkbox

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.

TaskDialog with hyperlink and verify checkbox

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.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionUnsafe code is not needed Pin
Mihajlo Cvetanovic25-Nov-16 3:11
memberMihajlo Cvetanovic25-Nov-16 3:11 
BugBugFix: EnableButton() and EnableRadioButton() on ActiveTaskDialog Pin
Alex Essilfie19-Nov-15 18:13
memberAlex Essilfie19-Nov-15 18:13 
BugDoesn't work, error message Pin
Axel Steiner13-Aug-15 2:21
memberAxel Steiner13-Aug-15 2:21 
AnswerRe: Doesn't work, error message Pin
Alex Essilfie19-Nov-15 15:01
memberAlex Essilfie19-Nov-15 15:01 
GeneralRe: Doesn't work, error message Pin
Balázs Lengyel2-Nov-17 6:55
memberBalázs Lengyel2-Nov-17 6:55 
BugStill System.ArgumentException! Pin
Knorx Thieus11-Aug-15 2:29
memberKnorx Thieus11-Aug-15 2:29 
GeneralVote of 5 Pin
Member 807878515-Jun-12 12:51
memberMember 807878515-Jun-12 12:51 
GeneralMy vote of 5 Pin
eaviles30-Mar-12 9:06
membereaviles30-Mar-12 9:06 
QuestionFont Changing... And A Question Pin
Saeed Masoomi7-Dec-11 11:39
memberSaeed Masoomi7-Dec-11 11:39 
GeneralLicense Pin
user303425-Feb-11 1:31
memberuser303425-Feb-11 1:31 
GeneralCitrix and expanded info Pin
Stefanie Knapp17-Jul-09 1:57
memberStefanie Knapp17-Jul-09 1:57 
QuestionHow change the MainInstruction text Pin
melnac9-Jan-09 12:57
membermelnac9-Jan-09 12:57 
GeneralIssue in parameter to EnableButton() and EnableRadioButton() when calling from VB.NET Pin
AndrewCushen18-Dec-08 17:58
memberAndrewCushen18-Dec-08 17:58 
This is great work, and is much appreciated; I started to do this myself in VB.NET, but ran out of time and kept hitting walls with the unsafe code.

You may, however, wish to document that there's a minor issue in the EnableButton() and EnableRadioButton() functions in ActiveTaskDialog.CS, when you call the code from VB.NET:

The True/False test appears to be reversed when calling these two functions from VB.NET, so that when you pass in the button id and False, the button is enabled. This is contrary to your documentation, which states that passing in False should disable the button, and is also contrary to the behavior when calling your code from C#, where it exhibits the proper behavior.

I can only guess that this has to do with the differing way in which the C-based languages treat 0/1 as true/false.

With that in mind, there are two approaches a VB.NET user can take to work around the issue.

The first, and simplest, is to just remember to reverse True/False when calling the 2 functions from VB.NET.

The second is to actually change the code in the C# source to match the documentation. This approach is probably preferable for the VB.NET programmer if and only if they are 100% certain the code will never be called from C#.

For those who choose the second option, the following line in both functions:
(IntPtr)(enable ? 0 : 1 ));

should be changed to read:
(IntPtr)(enable ? 1 : 0 ));

It is extremely important that the user document whichever approach they choose; the "enable" parameter in the XML comment above the function in the source code file should be changed to indicate either that VB.NET users should reverse the Boolean logic, or that C# users should.

GeneralShield Icon on CommandButton Pin
BYoung@OSIsoft.com13-Mar-08 14:04
memberBYoung@OSIsoft.com13-Mar-08 14:04 
QuestionLicensing Question Pin
Thorsten Dittmar15-Nov-07 2:52
memberThorsten Dittmar15-Nov-07 2:52 
GeneralWrapper and Emulator for pre-Vista Pin
Hedley Muscroft12-Nov-07 12:49
memberHedley Muscroft12-Nov-07 12:49 
QuestionHow to change the Button-Text Pin
Rekire31-Oct-07 14:43
memberRekire31-Oct-07 14:43 
AnswerRe: How to change the Button-Text Pin
KevinGre31-Oct-07 15:02
memberKevinGre31-Oct-07 15:02 
Generalerror [modified] Pin
pravin parmar27-Oct-07 5:35
memberpravin parmar27-Oct-07 5:35 
GeneralThanks for the effort Pin
rykk..15-Feb-07 18:42
memberrykk..15-Feb-07 18:42 3.0 support for task dialogs, custom Windows XP SP2 support ? Pin
Chris Richner9-Feb-07 1:59
memberChris Richner9-Feb-07 1:59 
AnswerRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? Pin
Jonas Nordlund2-Sep-07 23:42
memberJonas Nordlund2-Sep-07 23:42 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? [modified] Pin
Chris Richner3-Sep-07 7:43
memberChris Richner3-Sep-07 7:43 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? [modified] Pin
Jonas Nordlund4-Oct-07 0:39
memberJonas Nordlund4-Oct-07 0:39 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? Pin
Hedley Muscroft12-Nov-07 12:47
memberHedley Muscroft12-Nov-07 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 5 Jan 2007
Article Copyright 2007 by KevinGre
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid