Click here to Skip to main content
Click here to Skip to main content

TaskDialog for WinForms

By , 5 Jan 2007
 

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)
    {
        // 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.

License

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

About the Author

KevinGre
Software Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralVote of 5memberMember 807878515 Jun '12 - 11:51 
GeneralMy vote of 5membereaviles30 Mar '12 - 8:06 
QuestionFont Changing... And A QuestionmemberSaeed Masoomi7 Dec '11 - 10:39 
GeneralLicensememberuser303425 Feb '11 - 0:31 
GeneralCitrix and expanded infomemberStefanie Knapp17 Jul '09 - 0:57 
First of all, thanks for this great work!
 
But I have got one problem with it:
The dialog does not seem to appear on Citrix systems when it contains expanded info.
 
Anyone who has got the same problem? What can I do?
 
Thanks!
QuestionHow change the MainInstruction textmembermelnac9 Jan '09 - 11:57 
GeneralIssue in parameter to EnableButton() and EnableRadioButton() when calling from VB.NETmemberAndrewCushen18 Dec '08 - 16:58 
GeneralShield Icon on CommandButtonmemberBYoung@OSIsoft.com13 Mar '08 - 13:04 
QuestionLicensing QuestionmemberThorsten Dittmar15 Nov '07 - 1:52 
GeneralWrapper and Emulator for pre-VistamemberHedley Muscroft12 Nov '07 - 11:49 
QuestionHow to change the Button-TextmemberRekire31 Oct '07 - 13:43 
AnswerRe: How to change the Button-TextmemberKevinGre31 Oct '07 - 14:02 
Generalerror [modified]memberpravin parmar27 Oct '07 - 4:35 
GeneralThanks for the effortmemberrykk..15 Feb '07 - 17:42 
Question.net 3.0 support for task dialogs, custom Windows XP SP2 support ?memberChris Richner9 Feb '07 - 0:59 
AnswerRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ?memberJonas Nordlund2 Sep '07 - 22:42 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? [modified]memberChris Richner3 Sep '07 - 6:43 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ? [modified]memberJonas Nordlund3 Oct '07 - 23:39 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ?memberHedley Muscroft12 Nov '07 - 11:47 
GeneralRe: .net 3.0 support for task dialogs, custom Windows XP SP2 support ?memberJonas Nordlund26 Mar '08 - 15:30 
QuestionArgumentException in 64bit version?memberMember #10918077 Feb '07 - 1:59 
AnswerRe: ArgumentException in 64bit version?memberKevinGre7 Feb '07 - 7:58 
GeneralRe: ArgumentException in 64bit version?memberBrian C. Hart, Ph.D.21 Oct '09 - 7:52 
GeneralRe: ArgumentException in 64bit version?memberBrian C. Hart, Ph.D.21 Oct '09 - 7:55 
GeneralI see it as a MessageBox replacement/extensionmemberreddog_aw9 Jan '07 - 1:05 
GeneralRe: I see it as a MessageBox replacement/extensionmemberKevinGre9 Jan '07 - 8:46 
GeneralERROR! An error has occured. If an error has occured, please resolve the error and try again.memberPixelated8 Jan '07 - 17:30 
GeneralRe: ERROR! An error has occured. If an error has occured, please resolve the error and try again.memberPixelated8 Jan '07 - 17:33 
QuestionExcellent, but one Bug...memberfmaeseele8 Jan '07 - 10:08 
AnswerRe: Excellent, but one Bug...memberKevinGre8 Jan '07 - 12:17 
AnswerRe: Excellent, but one Bug...memberfmaeseele13 Jan '07 - 0:53 
GeneralExcellentmemberLeonJ8 Jan '07 - 5:13 
GeneralRe: ExcellentmemberRich Anderson2 Feb '07 - 20:19 

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

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