Windows forms is not cross platform but for Windows Forms look at TaskDialog. Some examples. The Owner parameter can be the form or any visible control on the form.
namespace ErrorExampleApp.Classes;
public static class CustomDialogs
{
public static void ErrorBox(Control owner, Exception exception, string buttonText = "OK")
{
TaskDialogButton button = new(buttonText);
var text = $"Encountered the following{Environment.NewLine}{exception.Message}";
TaskDialogPage page = new()
{
Caption = "Error",
SizeToContent = true,
Heading = text,
Icon = TaskDialogIcon.Error,
Buttons = [button]
};
TaskDialog.ShowDialog(owner, page);
}
public static void InformationBox(Control owner, string heading, string buttonText = "Ok")
{
TaskDialogButton okayButton = new(buttonText);
TaskDialogPage page = new()
{
Caption = "Information",
SizeToContent = true,
Heading = heading,
Icon = TaskDialogIcon.Information,
Buttons = [okayButton]
};
TaskDialog.ShowDialog(owner, page);
}
public static bool Question(Control owner, string heading)
{
TaskDialogButton yesButton = new("Yes") { Tag = DialogResult.Yes };
TaskDialogButton noButton = new("No") { Tag = DialogResult.No };
var buttons = new TaskDialogButtonCollection
{
noButton,
yesButton
};
TaskDialogPage page = new()
{
Caption = "Question",
SizeToContent = true,
Heading = heading,
Icon = TaskDialogIcon.Information,
Buttons = buttons
};
var result = TaskDialog.ShowDialog(owner, page);
return (DialogResult)result.Tag! == DialogResult.Yes;
}
}
Usage
private void ErrorSampleButton_Click(object sender, EventArgs e)
{
try
{
var lines = File.ReadAllLines("C:\\Files\\Payne.txt");
}
catch (Exception exception)
{
CustomDialogs.ErrorBox(this, exception);
}
}
You can use your own images for the Icon property from project resources e.g.
public static void ErrorBox(Control owner, Exception exception, string buttonText = "OK")
{
TaskDialogButton button = new(buttonText);
var text = $"Encountered the following{Environment.NewLine}{exception.Message}";
TaskDialogPage page = new()
{
Caption = "Error",
SizeToContent = true,
Heading = text,
Icon = new TaskDialogIcon(Properties.Resources.Explaination),
Buttons = [button]
};
TaskDialog.ShowDialog(owner, page);
}
Example