Home > OS >  C# MessageBoxManager-Changing message box names not working
C# MessageBoxManager-Changing message box names not working

Time:07-25

I tried to create message box in C# and customize the button's text with message box manager but it still doesn't changing the button's text
I am using c# .net6
MessageBoxManager changes the text of the buttons
my code:

MessageBoxManager.Yes = "Alright";
MessageBoxManager.No = "never";
string message = "test";
MessageBoxManager.Register();
MessageBox.Show(message, "test.exe", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
MessageBoxManager.Unregister();

MessageBoxManager
If someone can help me using MessageBoxManager i will appreciate it, although answers without it are good too

CodePudding user response:

With .NET Core 5 and higher there is TaskDialog Class.

See TechNet Wiki article.

Simple example for a yes/no returning a bool.

public class Dialogs
{
 
    /// <summary>
    /// Dialog to ask a question
    /// </summary>
    /// <param name="caption">text for dialog caption</param>
    /// <param name="heading">text for dialog heading</param>
    /// <param name="yesText">text for yes button</param>
    /// <param name="noText">text for no button</param>
    /// <param name="defaultButton">specifies the default button for this dialog</param>
    /// <returns>true for yes button, false for no button</returns>
    public static bool Question(string caption, string heading, string yesText, string noText, DialogResult defaultButton)
    {

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

        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
        };


        TaskDialogButton result = TaskDialog.ShowDialog(page);

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

    }

    [ModuleInitializer]
    public static void Init()
    {
        Application.EnableVisualStyles();
    }
}

Overload to center parent to center on and use a different icon.

public static bool Question(Control owner, string heading, Icon icon, DialogResult defaultButton = DialogResult.Yes)
{

    TaskDialogButton yesButton = new("Yes") { Tag = DialogResult.Yes };
    TaskDialogButton noButton = new("No") { 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 = "Question",
        SizeToContent = true,
        Heading = heading,
        Icon = new TaskDialogIcon(icon),
        Buttons = buttons
    };

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

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

}
  • Related