Using the TaskDialog (WindowsAPICodePack)

In the last few weeks i started a tutorial series on YouTube on Developing for Windows 7 using the WindowsAPICodePack.

Anyway here is the codesnippets that i show in my tutorials.

Developing for windows 7: Task Dialog #1 :

            using Microsoft.WindowsAPICodePack;
            using Microsoft.WindowsAPICodePack.Dialogs;

            TaskDialog td = new TaskDialog();
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            td.Show();

Developing for windows 7: Task Dialog #2:

Adding Stranded Buttons:

TaskDialog td = new TaskDialog();
td.StandardButtons = TaskDialogStandardButtons.Ok | TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.Retry | TaskDialogStandardButtons.No | TaskDialogStandardButtons.Close | TaskDialogStandardButtons.Cancel;
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.Show();

Capturing what the user presses:

            TaskDialog td = new TaskDialog();
            td.StandardButtons = TaskDialogStandardButtons.Ok | TaskDialogStandardButtons.Cancel;
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            TaskDialogResult tdr = td.Show();
            if (tdr == TaskDialogResult.Ok)
            {
                MessageBox.Show("Ok presed");
            }
            else if (tdr == TaskDialogResult.Cancel)
            {
                MessageBox.Show("Cancle presed");
            }

Developing for windows 7: Task Dialog #3:

Custom Buttons:

TaskDialog td = new TaskDialog();
TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
custonbtn.Click += new EventHandler(custonbtn_Click);
td.Controls.Add(custonbtn);
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.Show();

Capturing what the user presses: (Please note that we are faking a button press event here)

TaskDialog td = new TaskDialog();
TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
custonbtn.Click += new EventHandler(custonbtn_Click);
TaskDialogButton custonbtn2 = new TaskDialogButton("btnCustom2", "This is a custom btn");
td.Controls.Add(custonbtn);
td.Controls.Add(custonbtn2);
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
TaskDialogResult tdr = td.Show();
MessageBox.Show(tdr.ToString());

void custonbtn_Click(object sender, EventArgs e)
{
    MessageBox.Show("Custom button was pressed");
    TaskDialogButton tdb = (TaskDialogButton)sender;
    ((TaskDialog)tdb.HostingDialog).Close(TaskDialogResult.Ok);
}

Developing for windows 7: Task Dialog #4:

Adding RadioButtons

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
TaskDialogRadioButton trb = new TaskDialogRadioButton("rbtBtn1", "Radio button 1");
td.Controls.Add(trb);
trb.Default = true;
TaskDialogRadioButton trb1 = new TaskDialogRadioButton("rbtBtn2", "Radio button 2");
td.Controls.Add(trb1);
TaskDialogRadioButton trb2 = new TaskDialogRadioButton("rbtBtn3", "Radio button 3");
td.Controls.Add(trb2);
td.Show();

Capturing what button the user presses: (Note that I’m using a trick to capture the input, this isn’t the proper way to do this)

            int choice = 0;
            TaskDialog td = new TaskDialog();
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            TaskDialogRadioButton trb = new TaskDialogRadioButton("rbtBtn1", "Radio button 1");
            trb.Default = true;
            trb.Click += (s, d) => choice = 0;
            td.Controls.Add(trb);
            TaskDialogRadioButton trb1 = new TaskDialogRadioButton("rbtBtn2", "Radio button 2");
            trb1.Click += (s, d) => choice = 1;
            td.Controls.Add(trb1);
            TaskDialogRadioButton trb2 = new TaskDialogRadioButton("rbtBtn3", "Radio button 3");
            trb2.Click += (s, d) => choice = 2;
            td.Controls.Add(trb2);
            TaskDialogResult tdr = td.Show();

            if (tdr == TaskDialogResult.Ok && choice == 0)
            {
                MessageBox.Show("radio button 1 selected");
            }
            else if (tdr == TaskDialogResult.Ok && choice == 1)
            {
                MessageBox.Show("radio button 2 selected");
            }
            else if (tdr == TaskDialogResult.Ok && choice == 2)
            {
                MessageBox.Show("radio button 3 selected");
            }

Developing for windows 7: Task Dialog #5:

TaskDialog td = new TaskDialog();
TaskDialogCommandLink cl = new TaskDialogCommandLink("cl", "Command link 1");
cl.Click += (s, d) => MessageBox.Show(cl.ToString() + " Pressed");
TaskDialogCommandLink cl1 = new TaskDialogCommandLink("cl", "Command link 2");
cl1.Click += (s, d) => td.Close(TaskDialogResult.Close);
cl1.Default = true;
td.Controls.Add(cl);
td.Controls.Add(cl1);
td.StandardButtons = TaskDialogStandardButtons.Ok;
td.FooterCheckBoxText = "Footer check box text";

////You cant add both Command links and custom buttons on the same taskdialog
//TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
//td.Controls.Add(custonbtn);

TaskDialogResult tdr = td.Show();
MessageBox.Show(tdr.ToString());

Footer Text (I forgot to add this to the YouTube video):

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.FooterText = "This text is in the footer";
td.Show();

Developing for windows 7: Task Dialog #6:

TaskDialog td = new TaskDialog();
td.Text = "Progress Bar";
TaskDialogProgressBar tdp = new TaskDialogProgressBar(0, 100, 0);
td.ProgressBar = tdp;

TaskDialogButton add = new TaskDialogButton("btn", "add progress");
add.Click += (s, d) => tdp.Value += 10;
td.Controls.Add(add);

TaskDialogButton ChangeState = new TaskDialogButton("btn", "Error State");
ChangeState.Click += (s, d) => tdp.State = TaskDialogProgressBarState.Error;
td.Controls.Add(ChangeState);
td.Show();

Developing for windows 7: Task Dialog #7 :

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.DetailsExpandedLabel = "Show Less";
td.DetailsCollapsedLabel = "Show More";
td.DetailsExpandedText = "More details";
td.ExpansionMode = TaskDialogExpandedDetailsLocation.ExpandFooter;
td.Show();

Developing for windows 7: Task Dialog #8:

Hyperlinks:

TaskDialog td = new TaskDialog();
td.HyperlinksEnabled = true;
td.Text = "Go to <a href=\"www.google.com\">Google</a>";
td.HyperlinkClick += (s, d) => System.Diagnostics.Process.Start("www.google.com");
td.Show();

HelpInvoked:

TaskDialog td = new TaskDialog();
td.Text = "Press F1";
td.HelpInvoked += (s, d) => MessageBox.Show("Help Goes here");
td.Show();

Shield icon:

TaskDialogButton btn = new TaskDialogButton("btn", "Shield Icon");
btn.UseElevationIcon = true;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: