public TaskDialogButton ShowDialog(IntPtr hwndOwner, TaskDialogPage page, TaskDialogStartupLocation startupLocation = TaskDialogStartupLocation.CenterOwner) { return(System.Windows.Forms.TaskDialog.ShowDialog(hwndOwner, page, startupLocation)); }
private static void ShowTaskDialogExample() { var dialogPage = new TaskDialogPage() { Title = "Example 1", Instruction = "Hello Task Dialog! 👍", Text = "Hi, this is <A HREF=\"link1\">the Content</A>.\nBlah blah blah…", Icon = TaskDialogStandardIcon.SecuritySuccessGreenBar, Footer = { Text = "This is the <A HREF=\"link2\">footer</A>.", Icon = TaskDialogStandardIcon.Warning, }, Expander = { Text = "Expanded Information!", ExpandFooterArea = true }, ProgressBar = new TaskDialogProgressBar(), CustomButtonStyle = TaskDialogCustomButtonStyle.CommandLinks, EnableHyperlinks = true, AllowCancel = true, CanBeMinimized = true, SizeToContent = true, }; dialogPage.Created += (s, e) => { Console.WriteLine("Main Contents created!"); }; dialogPage.Destroyed += (s, e) => { Console.WriteLine("Main Contents destroyed!"); }; dialogPage.Expander.ExpandedChanged += (s, e) => { Console.WriteLine("Expander Expanded Changed: " + dialogPage.Expander.Expanded); }; var dialog = new TaskDialog(dialogPage); dialog.Opened += (s, e) => { Console.WriteLine("Dialog opened!"); }; dialog.Shown += (s, e) => { Console.WriteLine("Dialog shown!"); }; dialog.Closing += (s, e) => { Console.WriteLine("Dialog closing!"); }; dialog.Closed += (s, e) => { Console.WriteLine("Dialog closed!"); }; //dialog.Activated += (s, e) => //{ // Console.WriteLine("Dialog activated!"); //}; //dialog.Deactivated += (s, e) => //{ // Console.WriteLine("Dialog deactivated!"); //}; dialogPage.ProgressBar.Value = 1; TaskDialogStandardButton buttonYes = dialogPage.StandardButtons.Add(TaskDialogResult.Yes); buttonYes.Enabled = false; TaskDialogStandardButton buttonNo = dialogPage.StandardButtons.Add(TaskDialogResult.No); // Add a hidden "Cancel" button so that we can get notified when the user // closes the dialog through the window's X button or with ESC (and could // cancel the close operation). TaskDialogStandardButton buttonCancelHidden = dialogPage.StandardButtons.Add(TaskDialogResult.Cancel); buttonCancelHidden.Visible = false; buttonCancelHidden.Click += (s, e) => { Console.WriteLine("Cancel clicked!"); }; long timerCount = 2; var dialogPageTimer = null as Timer; dialogPage.Created += (s, e) => { dialogPageTimer = new Timer() { Enabled = true, Interval = 200 }; dialogPageTimer.Tick += (s2, e2) => { // Update the progress bar if value <= 35. if (timerCount <= 35) { dialogPage.ProgressBar.Value = (int)timerCount; } else if (timerCount == 36) { dialogPage.ProgressBar.State = TaskDialogProgressBarState.Paused; } timerCount++; }; }; dialogPage.Destroyed += (s, e) => { dialogPageTimer.Dispose(); dialogPageTimer = null; }; dialogPage.HyperlinkClicked += (s, e) => { Console.WriteLine("Hyperlink clicked!"); TaskDialog.Show(dialog, "Clicked Hyperlink: " + e.Hyperlink, icon: TaskDialogStandardIcon.Information); }; // Create custom buttons that are shown as command links. TaskDialogCustomButton button1 = dialogPage.CustomButtons.Add("Change Icon + Enable Buttons ✔"); TaskDialogCustomButton button2 = dialogPage.CustomButtons.Add("Disabled Button 🎵🎶", "After enabling, can show a new dialog."); TaskDialogCustomButton button3 = dialogPage.CustomButtons.Add("Some Admin Action…", "Navigates to a new dialog page."); button3.ElevationRequired = true; TaskDialogStandardIcon nextIcon = TaskDialogStandardIcon.SecuritySuccessGreenBar; button1.Click += (s, e) => { Console.WriteLine("Button1 clicked!"); // Don't close the dialog. e.CancelClose = true; nextIcon++; // Set the icon and the content. dialogPage.Icon = nextIcon; dialogPage.Instruction = "Icon: " + nextIcon; // Enable the "Yes" button and the 3rd button when the checkbox is set. buttonYes.Enabled = true; button2.Enabled = true; }; button2.Enabled = false; button2.Click += (s, e) => { Console.WriteLine("Button2 clicked!"); // Don't close the main dialog. e.CancelClose = true; // Show a new Taskdialog that shows an incrementing number. var newPage = new TaskDialogPage() { Text = "This is a new non-modal dialog!", Icon = TaskDialogStandardIcon.Information, }; TaskDialogStandardButton buttonClose = newPage.StandardButtons.Add(TaskDialogResult.Close); TaskDialogStandardButton buttonContinue = newPage.StandardButtons.Add(TaskDialogResult.Continue); int number = 0; void UpdateNumberText(bool callUpdate = true) { // Update the instruction with the new number. newPage.Instruction = "Hi there! Number: " + number.ToString(); } UpdateNumberText(false); var newPageTimer = null as Timer; newPage.Created += (s2, e2) => { newPageTimer = new Timer() { Enabled = true, Interval = 200 }; newPageTimer.Tick += (s3, e3) => { number++; UpdateNumberText(); }; }; newPage.Destroyed += (s2, e2) => { newPageTimer.Dispose(); newPageTimer = null; }; buttonContinue.Click += (s2, e2) => { Console.WriteLine("New dialog - Continue Button clicked"); e2.CancelClose = true; number += 1000; UpdateNumberText(); }; var innerDialog = new TaskDialog(newPage); TaskDialogButton innerResult = innerDialog.Show(); Console.WriteLine("Result of new dialog: " + innerResult); }; button3.Click += (s, e) => { Console.WriteLine("Button3 clicked!"); // Don't close the dialog from the button click. e.CancelClose = true; // Create a new contents instance to which we will navigate the dialog. var newContents = new TaskDialogPage() { Instruction = "Page 2", Text = "Welcome to the second page!", Icon = TaskDialogStandardIcon.SecurityShieldBlueBar, SizeToContent = true, CheckBox = { Text = "I think I agree…" }, ProgressBar = { State = TaskDialogProgressBarState.Marquee } }; newContents.Created += (s2, e2) => { Console.WriteLine("New Contents created!"); // Set a new icon after navigating the dialog. This allows us to show the // yellow bar from the "SecurityWarningBar" icon with a different icon. newContents.Icon = TaskDialogStandardIcon.Warning; }; newContents.Destroyed += (s2, e2) => { Console.WriteLine("New Contents destroyed!"); }; TaskDialogStandardButton buttonCancel = newContents.StandardButtons.Add(TaskDialogResult.Cancel); buttonCancel.Enabled = false; buttonCancel.ElevationRequired = true; // Create a custom button that will be shown as regular button. TaskDialogCustomButton customButton = newContents.CustomButtons.Add("My Button :)"); // Add radio buttons. TaskDialogRadioButton radioButton1 = newContents.RadioButtons.Add("My Radio Button 1"); TaskDialogRadioButton radioButton2 = newContents.RadioButtons.Add("My Radio Button 2"); radioButton2.Checked = true; radioButton1.CheckedChanged += (s2, e2) => Console.WriteLine( $"Radio Button 1 CheckedChanged: RB1={radioButton1.Checked}, RB2={radioButton2.Checked}"); radioButton2.CheckedChanged += (s2, e2) => Console.WriteLine( $"Radio Button 2 CheckedChanged: RB1={radioButton1.Checked}, RB2={radioButton2.Checked}"); newContents.CheckBox.CheckedChanged += (s2, e2) => { Console.WriteLine("Checkbox CheckedChanged: " + newContents.CheckBox.Checked); buttonCancel.Enabled = newContents.CheckBox.Checked; }; // Now navigate the dialog. dialog.Page = newContents; }; TaskDialogButton result = dialog.Show(); Console.WriteLine("Result of main dialog: " + result); }
private void ShowMultiPageTaskDialog() { // Disable the "Yes" button and only enable it when the check box is checked. // Also, don't close the dialog when this button is clicked. var initialButtonYes = TaskDialogButton.Yes; initialButtonYes.Enabled = false; initialButtonYes.AllowCloseDialog = false; var initialPage = new TaskDialogPage() { Caption = "My Application", Heading = "Clean up database?", Text = "Do you really want to do a clean-up?\nThis action is irreversible!", Icon = TaskDialogIcon.ShieldWarningYellowBar, AllowCancel = true, // A modeless dialog can be minimizable. AllowMinimize = true, Verification = new TaskDialogVerificationCheckBox() { Text = "I know what I'm doing" }, Buttons = { TaskDialogButton.No, initialButtonYes }, DefaultButton = TaskDialogButton.No }; // For the "In Progress" page, don't allow the dialog to close, by adding // a disabled button (if no button was specified, the task dialog would // get an (enabled) 'OK' button). var inProgressCloseButton = TaskDialogButton.Close; inProgressCloseButton.Enabled = false; var inProgressPage = new TaskDialogPage() { Caption = "My Application", Heading = "Operation in progress...", Text = "Please wait while the operation is in progress.", Icon = TaskDialogIcon.Information, AllowMinimize = true, ProgressBar = new TaskDialogProgressBar() { State = TaskDialogProgressBarState.Marquee }, Expander = new TaskDialogExpander() { Text = "Initializing...", Position = TaskDialogExpanderPosition.AfterFootnote }, Buttons = { inProgressCloseButton } }; // Add an invisible Cancel button where we will intercept the Click event // to prevent the dialog from closing (when the User clicks the "X" button // in the title bar or presses ESC or Alt+F4). var invisibleCancelButton = TaskDialogButton.Cancel; invisibleCancelButton.Visible = false; invisibleCancelButton.AllowCloseDialog = false; inProgressPage.Buttons.Add(invisibleCancelButton); var finishedPage = new TaskDialogPage() { Caption = "My Application", Heading = "Success!", Text = "The operation finished.", Icon = TaskDialogIcon.ShieldSuccessGreenBar, AllowMinimize = true, Buttons = { TaskDialogButton.Close } }; TaskDialogButton showResultsButton = new TaskDialogCommandLinkButton("Show &Results"); finishedPage.Buttons.Add(showResultsButton); // Enable the "Yes" button only when the checkbox is checked. TaskDialogVerificationCheckBox checkBox = initialPage.Verification; checkBox.CheckedChanged += (sender, e) => { initialButtonYes.Enabled = checkBox.Checked; }; // When the user clicks "Yes", navigate to the second page. initialButtonYes.Click += (sender, e) => { // Navigate to the "In Progress" page that displays the // current progress of the background work. initialPage.Navigate(inProgressPage); // NOTE: When you implement a "In Progress" page that represents // background work that is done e.g. by a separate thread/task, // which eventually calls Control.Invoke()/BeginInvoke() when // its work is finished in order to navigate or update the dialog, // then DO NOT start that work here already (directly after // setting the Page property). Instead, start the work in the // TaskDialogPage.Created event of the new page. // // See comments in the code sample in https://github.com/dotnet/winforms/issues/146 // for more information. }; // Simulate work by starting an async operation from which we are updating the // progress bar and the expander with the current status. inProgressPage.Created += async(s, e) => { // Run the background operation and iterate over the streamed values to update // the progress. Because we call the async method from the GUI thread, // it will use this thread's synchronization context to run the continuations, // so we don't need to use Control.[Begin]Invoke() to schedule the callbacks. var progressBar = inProgressPage.ProgressBar; await foreach (int progressValue in StreamBackgroundOperationProgressAsync()) { // When we display the first progress, switch the marquee progress bar // to a regular one. if (progressBar.State == TaskDialogProgressBarState.Marquee) { progressBar.State = TaskDialogProgressBarState.Normal; } progressBar.Value = progressValue; inProgressPage.Expander.Text = $"Progress: {progressValue} %"; } // Work is finished, so navigate to the third page. inProgressPage.Navigate(finishedPage); }; // Show the dialog (modeless). TaskDialogButton result = TaskDialog.ShowDialog(initialPage); if (result == showResultsButton) { Console.WriteLine("Showing Results!"); }
private void ShowSimpleTaskDialog() { // Show a message box. DialogResult messageBoxResult = MessageBox.Show( this, text: "Stopping the operation might leave your database in a corrupted state. Are you sure you want to stop?", caption: "Confirmation [Message Box]", buttons: MessageBoxButtons.YesNo, icon: MessageBoxIcon.Warning, defaultButton: MessageBoxDefaultButton.Button2); if (messageBoxResult == DialogResult.Yes) { Console.WriteLine("User confirmed to stop the operation."); } // Show a task dialog (simple). TaskDialogButton result = TaskDialog.ShowDialog(this, new TaskDialogPage() { Text = "Stopping the operation might leave your database in a corrupted state.", Heading = "Are you sure you want to stop?", Caption = "Confirmation (Task Dialog)", Buttons = { TaskDialogButton.Yes, TaskDialogButton.No }, Icon = TaskDialogIcon.Warning, DefaultButton = TaskDialogButton.No }); if (result == TaskDialogButton.Yes) { Console.WriteLine("User confirmed to stop the operation."); } // Show a task dialog (enhanced). var page = new TaskDialogPage() { Heading = "Are you sure you want to stop?", Text = "Stopping the operation might leave your database in a corrupted state.", Caption = "Confirmation (Task Dialog)", Icon = TaskDialogIcon.Warning, AllowCancel = true, Verification = new TaskDialogVerificationCheckBox() { Text = "Do not show again" }, Buttons = { TaskDialogButton.Yes, TaskDialogButton.No }, DefaultButton = TaskDialogButton.No }; var resultButton = TaskDialog.ShowDialog(this, page); if (resultButton == TaskDialogButton.Yes) { if (page.Verification.Checked) { Console.WriteLine("Do not show this confirmation again."); } Console.WriteLine("User confirmed to stop the operation."); } }
private void ShowAutoClosingTaskDialog() { const string textFormat = "Reconnecting in {0} seconds..."; int remainingTenthSeconds = 50; var reconnectButton = new TaskDialogButton("&Reconnect now"); var cancelButton = TaskDialogButton.Cancel; var page = new TaskDialogPage() { Heading = "Connection lost; reconnecting...", Text = string.Format(textFormat, (remainingTenthSeconds + 9) / 10), // Display the form's icon in the task dialog. // Note however that the task dialog will not scale the icon. Icon = new TaskDialogIcon(this.Icon), ProgressBar = new TaskDialogProgressBar() { State = TaskDialogProgressBarState.Paused }, Buttons = { reconnectButton, cancelButton } }; // Create a WinForms timer that raises the Tick event every tenth second. using (var timer = new Timer() { Enabled = true, Interval = 100 }) { timer.Tick += (s, e) => { remainingTenthSeconds--; if (remainingTenthSeconds > 0) { // Update the remaining time and progress bar. page.Text = string.Format(textFormat, (remainingTenthSeconds + 9) / 10); page.ProgressBar.Value = 100 - remainingTenthSeconds * 2; } else { // Stop the timer and click the "Reconnect" button - this will // close the dialog. timer.Enabled = false; reconnectButton.PerformClick(); } }; TaskDialogButton result = TaskDialog.ShowDialog(this, page); if (result == reconnectButton) { Console.WriteLine("Reconnecting."); } else { Console.WriteLine("Not reconnecting."); } } }