Ever need to display a message box for “Do not show again”, “auto-close”, “Open links”, specify how many buttons on a dialog along with the text? These are just a few example found in the following GitHub repository which shows how to use these options in both Windows Forms and WPF projects using a common class project created with .NET Core, C# 9.
For VB.NET see the following code sample.
Suppose there is a need for displaying a dialog that needs to perform actions?
Specify a custom Icon (here hard coded while there are other samples that accept an Icon to be fluid), an option to center the dialog on a form or window? The following does both along with setting the default button.Code:public static void Question(Control owner, string heading, Action yesAction, Action noAction) { TaskDialogButton yesButton = new("Yes") { Tag = DialogResult.Yes }; TaskDialogButton noButton = new("No") { Tag = DialogResult.No }; var buttons = new TaskDialogButtonCollection { yesButton, noButton }; TaskDialogPage page = new() { Caption = "Question", SizeToContent = true, Heading = heading, Icon = new TaskDialogIcon(Properties.Resources.QuestionBlue), Buttons = buttons }; var result = TaskDialog.ShowDialog(owner, page); if ((DialogResult)result.Tag == DialogResult.Yes) { yesAction?.Invoke(); } else { noAction?.Invoke(); } }
Do not show again code works with a json configuration file with code to abide by the user's choice and method to reset.Code:public static bool Question(IntPtr owner, string caption, string heading, string yesText, string noText, DialogResult defaultButton = DialogResult.No) { 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 = new TaskDialogIcon(Properties.Resources.question32), Buttons = buttons }; var result = TaskDialog.ShowDialog(owner, page, TaskDialogStartupLocation.CenterOwner); return (DialogResult)result.Tag == DialogResult.Yes; }
Auto-close, in this case partly hard coded with room to expand.Code:public static (NoShowResult DialogResult, bool showAgain) DoNotShowAgain(ShowAgainOptions options) { TaskDialogPage page = new () { Heading = options.Heading, Text = options.Text, Caption = options.Caption, Icon = new TaskDialogIcon(options.Icon), AllowCancel = true, Verification = new TaskDialogVerificationCheckBox() { Text = options.VerificationText }, Buttons = new TaskDialogButtonCollection() { TaskDialogButton.Yes, TaskDialogButton.No }, DefaultButton = TaskDialogButton.No }; if (TaskDialog.ShowDialog(options.IntPtr,page) == TaskDialogButton.Yes) { bool showAgain = false; if (page.Verification.Checked) { SettingOperations.SetShowAgain(false); showAgain = false; } else { SettingOperations.SetShowAgain(true); showAgain = true; } return (NoShowResult.StopOperation, showAgain); } else { return (NoShowResult.No, true); } }
Code:public static void AutoCloseDialog(IntPtr owner, Icon Icon, int seconds, string text, string okText = "OK", string cancelText = "Cancel") { var remaining = seconds * 10; TaskDialogButton continueButton = new(okText); TaskDialogButton cancelButton = new(cancelText); TaskDialogPage page = new() { Heading = "To cancel select the cancel button", Text = text, Icon = new TaskDialogIcon(Icon), Buttons = new TaskDialogButtonCollection() { continueButton, cancelButton }, Caption = "Data operations" }; using Timer timer = new() { Enabled = true, Interval = 100 }; timer.Tick += (_, _) => { remaining -= 1; if (remaining != 0) return; timer.Enabled = false; if (continueButton.BoundPage is not null) { continueButton.PerformClick(); } }; TaskDialogButton result = TaskDialog.ShowDialog(owner, page); ContinueOperation?.Invoke(result == continueButton); }




Reply With Quote
