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:

To get a better idea of all the things you can do with a TaskDialogdownload 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.

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.

I'm using the TaskDialog for asking the user of my app if they want to restart the application to do the requested task because it requires elevated access, similiar to how VS 2008 asks when you try to do something that requires elevated access.

For the CommandButton that is for saying "Yes, elevate me", I would like to show the shield icon instead of just the standard green arrow. But I've yet to figure out how. I'm pretty sure it's possible because VS2008 does this, but who knows what VS is doing underneath the hood.

Just to let you know, I recently posted an article which builds on your superb project by including an 'emulator' so that the TaskDialog can be used pre-Vista too. I've given you full credit for this excellent article though, and included links to it from my article. I hope that's ok.

You can't. If you look at the MSDN documentation for TaskDialog (link below) you can see that TaskDialog does not support updating the text of buttons (which is often a poor experience for your users anyway). You might consider having two buttons and using enable and disable, or if you want complete control creating your own dialog. Remember, TaskDialog is designed to meet a lot of requirements and save you creating custom dialogs for most common tasks but it is not the answer to all needs. Technically you could use the window handle of the active TaskDialog to get the window handle of buttons and change their text but that would be pretty evil and probably technically unsupported which means it might break on future versions (adjust layout correctly etc).

I'm wondering why .net 3.0 framework has no managed way to use task dialogs. When does MS extend .net to provide managed support for vista platform features?

Anybody tought about an legacy implementation of ITaskDialog for task dialogs in .net 2.0 running on windows xp sp2. Maybe there could be something like a migration path for code running currently on xp using custom task dialog implementations today but will run with slightly changes on a future .net framework for vista system.

Agreed; I would love to have a pretty and functional fallback using a custom form for Windows XP. I simply cannot use the new TaskDialog if the dialogs on the OS with the currently still major user base will have reduced functionality. A legacy implementation, using the "real" API under Vista, would be most welcome here, although I understand if there is some effort involved in achieving that...

Scroll to the bottom of that page, and download the sample application they made. I think the last one is a commercial component, but it looks pretty good and I could not even distinguish the difference easily, at least when running the app on Vista and comparing it with the native mode box. (I believe the control suppports some flag that tells if it should use the native TaskDialog API if available)

I don't mean to be nag but 2010 is upon us and it looks like you wrote this article (very good btw! thanks!) in 2007....I downloaded the source and got the ArgumentException error discussed in this thread....looks like the update still has not been posted....thanks for posting it!

> I look forward to seeing a lot fewer Message Boxes in you applications and I hope this makes it easier.

Perhaps I'm mistaken but Task Dialog's seem more like a potential MessageBox replacement (let's face it, no matter how hard you try you always end up with one of the pesky buggers in your UI anyway).

My thoughts are that perhaps this library would be useful in creating "standardised" MessageBox helper functions perhaps with some logic for platform detection to display either a MessageBox if we're in a non-vista executing environment or TaskDialog's with all the nice bell's and whistles if we are in Vista.