lior fogel
lior fogel

Reputation: 23

C# MessageBoxManager-Changing message box names not working

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

Upvotes: 1

Views: 662

Answers (3)

Karen Payne
Karen Payne

Reputation: 5117

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

See TechNet Wiki article (Microsoft terminate site).

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;

}

Upvotes: 2

David Watson
David Watson

Reputation: 41

I put C-Sharp Developer's answer into the MessageBoxManager code here if anyone wants it.

https://github.com/wyrdfish/ShareAndEnjoy/blob/master/MessageBoxManager/MessageBoxManager.cs

I added

    [DllImport("kernel32.dll")]
    static extern uint GetCurrentThreadId();

and then used it in the Register method

hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, (int)GetCurrentThreadId());

Its working for me on dotnet 8.

Upvotes: 0

C-Sharp Developer
C-Sharp Developer

Reputation: 151

The problem here is, that AppDomain.GetCurrentThreadId() in .Net 6/.NetCore returns the ManagedThreadId (tested with debugging). The Register()-method in the MessageBoxManager must be changed: AppDomain.GetCurrentThreadId() has to be replaced with a method to get the unmanaged ThreadId. This can be done for example with the native method shown here http://www.pinvoke.net/default.aspx/kernel32/GetCurrentThreadId.html.

Upvotes: 1

Related Questions