Click here to Skip to main content
15,894,740 members
Articles / Operating Systems / Windows

TaskDialog for WinForms

Rate me:
Please Sign up or sign in to vote.
4.81/5 (39 votes)
5 Jan 2007Public Domain2 min read 564.3K   6.6K   108  
Using Vista Task Dialog from WinForms - here is some code to do it.
//------------------------------------------------------------------
// <summary>
// A P/Invoke wrapper for TaskDialog. Usability was given preference to perf and size.
// </summary>
//
// <remarks/>
//------------------------------------------------------------------

namespace Microsoft.Samples
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// TaskDialog wrapped in a CommonDialog class. This is required to work well in
    /// MMC 3.0. In MMC 3.0 you must use the ShowDialog methods on the MMC classes to
    /// correctly show a modal dialog. This class will allow you to do this and keep access
    /// to the results of the TaskDialog.
    /// </summary>
    public class TaskDialogCommonDialog : CommonDialog
    {
        /// <summary>
        /// The TaskDialog we will display.
        /// </summary>
        private TaskDialog taskDialog;

        /// <summary>
        /// The result of the dialog, either a DialogResult value for common push buttons set in the TaskDialog.CommonButtons
        /// member or the ButtonID from a TaskDialogButton structure set on the TaskDialog.Buttons member.
        /// </summary>
        private int taskDialogResult;

        /// <summary>
        /// The verification flag result of the dialog. True if the verification checkbox was checked when the dialog
        /// was dismissed.
        /// </summary>
        private bool verificationFlagCheckedResult;

        /// <summary>
        /// TaskDialog wrapped in a CommonDialog class. THis is required to work well in
        /// MMC 2.1. In MMC 2.1 you must use the ShowDialog methods on the MMC classes to
        /// correctly show a modal dialog. This class will allow you to do this and keep access
        /// to the results of the TaskDialog.
        /// </summary>
        /// <param name="taskDialog">The TaskDialog to show.</param>
        public TaskDialogCommonDialog(TaskDialog taskDialog)
        {
            if (taskDialog == null)
            {
                throw new ArgumentNullException("taskDialog");
            }

            this.taskDialog = taskDialog;
        }

        /// <summary>
        /// The TaskDialog to show.
        /// </summary>
        public TaskDialog TaskDialog
        {
            get { return this.taskDialog; }
        }

        /// <summary>
        /// The result of the dialog, either a DialogResult value for common push buttons set in the TaskDialog.CommonButtons
        /// member or the ButtonID from a TaskDialogButton structure set on the TaskDialog.Buttons member.
        /// </summary>
        public int TaskDialogResult
        {
            get { return this.taskDialogResult; }
        }

        /// <summary>
        /// The verification flag result of the dialog. True if the verification checkbox was checked when the dialog
        /// was dismissed.
        /// </summary>
        public bool VerificationFlagCheckedResult
        {
            get { return this.verificationFlagCheckedResult; }
        }

        /// <summary>
        /// Reset the common dialog.
        /// </summary>
        public override void Reset()
        {
            this.taskDialog.Reset();
        }

        /// <summary>
        /// The required implementation of CommonDialog that shows the Task Dialog.
        /// </summary>
        /// <param name="hwndOwner">Owner window. This can be null.</param>
        /// <returns>If this method returns true, then ShowDialog will return DialogResult.OK.
        /// If this method returns false, then ShowDialog will return DialogResult.Cancel. The
        /// user of this class must use the TaskDialogResult member to get more information.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            this.taskDialogResult = this.taskDialog.Show(hwndOwner, out this.verificationFlagCheckedResult);
            return (this.taskDialogResult != (int)DialogResult.Cancel);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions