Mert Özdemir
Mert Özdemir

Reputation: 23

How to use TaskDialog in c# Windows Forms

Actually I need a messageBox with custom buttons, and I don't want to use another form or messageBoxManager as a messageBox. I thought maybe a TaskDialog help me but i don't know how to use it.

Upvotes: 0

Views: 2509

Answers (1)

Karen Payne
Karen Payne

Reputation: 5157

Check out Microsoft TechNet article with source .NET Core 5 TaskDialog (C#)

enter image description here

Ask a question

public static bool Question(Form owner, string caption, string heading, string yesText, string noText, DialogResult defaultButton)
{

    TaskDialogButton yesButton = new (yesText) { Tag = DialogResult.Yes };
    TaskDialogButton noButton = new (noText) { Tag = DialogResult.No };

    var buttons = new TaskDialogButtonCollection();

    if (defaultButton == DialogResult.Yes)
    {
        buttons.Add(yesButton);
        buttons.Add(noButton);
    }
    else
    {
        buttons.Add(noButton);
        buttons.Add(yesButton);
    }


    TaskDialogPage page = new ()
    {
        Caption = caption,
        SizeToContent = true,
        Heading = heading,
        Icon = TaskDialogIcon.Information,
        Buttons = buttons
    };

    var result = TaskDialog.ShowDialog(owner, page);

    return (DialogResult)result.Tag == DialogResult.Yes;

}

Show information

public static void Information(IntPtr owner, string heading, string buttonText = "Ok")
{

    TaskDialogButton okayButton = new(buttonText);

    TaskDialogPage page = new()
    {
        Caption = "Information",
        SizeToContent = true,
        Heading = heading,
        //Icon = TaskDialogIcon.Information, -- will invoke a beep
        Buttons = new TaskDialogButtonCollection() { okayButton }
    };
    
    TaskDialog.ShowDialog(owner, page);

}

Upvotes: 2

Related Questions