bala3569
bala3569

Reputation: 11010

How to change the button text for 'Yes' and 'No' buttons in the MessageBox.Show dialog?

I need to change the message box control buttons Yes to Continue and No to Close. How do I change the button text?

Here is my code:

 DialogResult dlgResult = MessageBox.Show("Patterns have been logged successfully", "Logtool", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

Upvotes: 83

Views: 233918

Answers (5)

bsch15
bsch15

Reputation: 1

MessageBoxManager was nearly perfect but I couldnt find a way to resize the buttons to accept longer text. So I referenced Greg Kowieski's answer to create my own that may help others as well. Create a new Windows Form named CustomMsgBox.cs

CustomMsgBox.cs:

using System.Linq;
using System.Windows.Forms;

namespace YourProjNamespace
{
   public partial class CustomMsgBox : Form
   {
      public CustomMsgBox(string text, string caption, string yesBtnText, string noBtnText, string cancelBtnText)
      {
         InitializeComponent();
         this.Text = !string.IsNullOrWhiteSpace(caption) ? caption : "";
         contentLabel.Text = text;

         buttonYes.Text = yesBtnText;
         bool displayNoButton = !string.IsNullOrWhiteSpace(noBtnText);
         buttonNo.Visible = displayNoButton;
         buttonNo.Text = (displayNoButton) ? noBtnText : buttonNo.Text;
         bool displayCancelButton = !string.IsNullOrWhiteSpace(cancelBtnText);
         buttonCancel.Visible = displayCancelButton;
         buttonCancel.Text = (displayCancelButton) ? cancelBtnText : buttonCancel.Text;

         //Ensure the form is large enough      
         int buttonWidths = buttonCancel.Width + buttonNo.Width + buttonYes.Width + (6 * 3) + 40; //Widths + Margins + Extra space to look pretty
         int[] widths = new int[] { contentLabel.Width, buttonPanel.Width, mainPanel.Width, bottomPanel.Width, buttonWidths };
         int minWidth = widths.Max();
         this.MinimumSize = new System.Drawing.Size(minWidth, bottomPanel.Height + mainPanel.Height + 20);

         buttonYes.DialogResult = DialogResult.Yes;
         buttonNo.DialogResult = DialogResult.No;
         buttonCancel.DialogResult = DialogResult.Cancel;
      }
      public static DialogResult Show(string text, MessageBoxButtons messageBoxButtons)
      {
         return Show(text, null, messageBoxButtons);
      }
      public static DialogResult Show(string text, string caption, MessageBoxButtons messageBoxButtons)
      {
         CustomMsgBox form;
         switch (messageBoxButtons)
         {
            case MessageBoxButtons.OKCancel:
               form = new CustomMsgBox(text, caption, "OK", null, "Cancel");
               form.buttonYes.DialogResult = DialogResult.OK;
               break;
            case MessageBoxButtons.AbortRetryIgnore:
               form = new CustomMsgBox(text, caption, "Abort", "Retry", "Ignore");
               form.buttonYes.DialogResult = DialogResult.Abort;
               form.buttonNo.DialogResult = DialogResult.Retry;
               form.buttonCancel.DialogResult = DialogResult.Ignore;
               break;
            case MessageBoxButtons.YesNoCancel:
               form = new CustomMsgBox(text, caption, "Yes", "No", "Cancel");
               break;
            case MessageBoxButtons.YesNo:
               form = new CustomMsgBox(text, caption, "Yes", "No", null);
               break;
            case MessageBoxButtons.RetryCancel:
               form = new CustomMsgBox(text, caption, "Retry", null, "Cancel");
               form.buttonYes.DialogResult = DialogResult.Retry;
               break;
            default:
            case MessageBoxButtons.OK:
               form = new CustomMsgBox(text, caption, "OK", null, null);
               form.buttonYes.DialogResult = DialogResult.OK;
               break;
         }

         DialogResult dialogResult = form.ShowDialog();
         return dialogResult;
      }
      public static DialogResult Show(string text)
      {
         return ShowDialog(text, null, "OK", null, null);
      }
      public static DialogResult Show(string text, string caption)
      {
         return ShowDialog(text, caption, "OK", null, null);
      }
      /// <summary>
      /// Dispalys a Message box with a DialogResult.Yes button with custom text
      /// </summary>
      /// <param name="text">The text of the MessageBox's body</param>
      /// <param name="caption">The text of the MessageBox's title</param>
      /// <param name="yesBtnText">Text of the DialogResult.Yes button</param>
      /// <returns></returns>
      public static DialogResult Show(string text, string caption, string yesBtnText)
      {
         return ShowDialog(text, caption, yesBtnText, null, null);
      }
      /// <summary>
      /// Dispalys a Message box with a DialogResult.Yes and DialogResult.No buttons with custom text
      /// </summary>
      /// <param name="text">The text of the MessageBox's body</param>
      /// <param name="caption">The text of the MessageBox's title</param>
      /// <param name="yesBtnText">Text of the DialogResult.Yes button</param>
      /// <param name="noBtnText">Text of the DialogResult.No button</param>
      /// <returns></returns>
      public static DialogResult Show(string text, string caption, string yesBtnText, string noBtnText)
      {
         return ShowDialog(text, caption, yesBtnText, noBtnText, null);
      }
      /// <summary>
      /// Dispalys a Message box with a DialogResult.Yes, DialogResult.No, and DialogResult.Cancel buttons
      /// with custom text
      /// </summary>
      /// <param name="text">The text of the MessageBox's body</param>
      /// <param name="caption">The text of the MessageBox's title</param>
      /// <param name="yesBtnText">Text of the DialogResult.Yes button</param>
      /// <param name="noBtnText">Text of the DialogResult.No button</param>
      /// <param name="cancelBtnText">Text of the DialogResult.Cancel button</param>
      /// <returns></returns>
      public static DialogResult Show(string text, string caption, string yesBtnText, string noBtnText, string cancelBtnText)
      {
         return ShowDialog(text, caption, yesBtnText, noBtnText, cancelBtnText);
      }

      private static DialogResult ShowDialog(string text, string caption, string yesBtnText, string noBtnText, string cancelBtnText)
      {
         CustomMsgBox form = new CustomMsgBox(text, caption, yesBtnText, noBtnText, cancelBtnText);
         DialogResult dialogResult = form.ShowDialog();
         return dialogResult;
      }
   }
}

CustomMsgBox.Designer.cs:

namespace YourProjNamespace
{
   partial class CustomMsgBox
   {
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.IContainer components = null;

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
         if (disposing && (components != null))
         {
            components.Dispose();
         }
         base.Dispose(disposing);
      }

      #region Windows Form Designer generated code

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.buttonPanel = new System.Windows.Forms.FlowLayoutPanel();
         this.buttonYes = new System.Windows.Forms.Button();
         this.buttonNo = new System.Windows.Forms.Button();
         this.buttonCancel = new System.Windows.Forms.Button();
         this.mainPanel = new System.Windows.Forms.TableLayoutPanel();
         this.contentLabel = new System.Windows.Forms.Label();
         this.bottomPanel = new System.Windows.Forms.TableLayoutPanel();
         this.buttonPanel.SuspendLayout();
         this.mainPanel.SuspendLayout();
         this.bottomPanel.SuspendLayout();
         this.SuspendLayout();
         // 
         // buttonPanel
         // 
         this.buttonPanel.AutoSize = true;
         this.buttonPanel.BackColor = System.Drawing.SystemColors.ControlLight;
         this.buttonPanel.Controls.Add(this.buttonCancel);
         this.buttonPanel.Controls.Add(this.buttonNo);
         this.buttonPanel.Controls.Add(this.buttonYes);
         this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Fill;
         this.buttonPanel.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
         this.buttonPanel.Location = new System.Drawing.Point(3, 5);
         this.buttonPanel.MaximumSize = new System.Drawing.Size(640, 31);
         this.buttonPanel.Name = "buttonPanel";
         this.buttonPanel.Size = new System.Drawing.Size(278, 31);
         this.buttonPanel.TabIndex = 0;
         // 
         // buttonYes
         // 
         this.buttonYes.AutoSize = true;
         this.buttonYes.FlatStyle = System.Windows.Forms.FlatStyle.System;
         this.buttonYes.Font = new System.Drawing.Font("Segoe UI", 9F);
         this.buttonYes.Location = new System.Drawing.Point(205, 3);
         this.buttonYes.MinimumSize = new System.Drawing.Size(70, 20);
         this.buttonYes.Name = "buttonYes";
         this.buttonYes.Size = new System.Drawing.Size(70, 25);
         this.buttonYes.TabIndex = 0;
         this.buttonYes.Text = "Yes";
         this.buttonYes.UseVisualStyleBackColor = true;
         // 
         // buttonNo
         // 
         this.buttonNo.AutoSize = true;
         this.buttonNo.BackColor = System.Drawing.SystemColors.ControlLight;
         this.buttonNo.FlatStyle = System.Windows.Forms.FlatStyle.System;
         this.buttonNo.Font = new System.Drawing.Font("Segoe UI", 9F);
         this.buttonNo.Location = new System.Drawing.Point(124, 3);
         this.buttonNo.Name = "buttonNo";
         this.buttonNo.Size = new System.Drawing.Size(75, 25);
         this.buttonNo.TabIndex = 1;
         this.buttonNo.Text = "No";
         this.buttonNo.UseVisualStyleBackColor = false;
         // 
         // buttonCancel
         // 
         this.buttonCancel.AutoSize = true;
         this.buttonCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
         this.buttonCancel.Font = new System.Drawing.Font("Segoe UI", 9F);
         this.buttonCancel.Location = new System.Drawing.Point(48, 3);
         this.buttonCancel.Name = "buttonCancel";
         this.buttonCancel.Size = new System.Drawing.Size(70, 25);
         this.buttonCancel.TabIndex = 2;
         this.buttonCancel.Text = "Cancel";
         this.buttonCancel.UseVisualStyleBackColor = true;
         // 
         // mainPanel
         // 
         this.mainPanel.AutoSize = true;
         this.mainPanel.BackColor = System.Drawing.SystemColors.Window;
         this.mainPanel.ColumnCount = 1;
         this.mainPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
         this.mainPanel.Controls.Add(this.contentLabel, 0, 1);
         this.mainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
         this.mainPanel.Location = new System.Drawing.Point(0, 0);
         this.mainPanel.MinimumSize = new System.Drawing.Size(275, 58);
         this.mainPanel.Name = "mainPanel";
         this.mainPanel.Padding = new System.Windows.Forms.Padding(10, 5, 10, 5);
         this.mainPanel.RowCount = 3;
         this.mainPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
         this.mainPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
         this.mainPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
         this.mainPanel.Size = new System.Drawing.Size(284, 64);
         this.mainPanel.TabIndex = 1;
         // 
         // contentLabel
         // 
         this.contentLabel.AutoSize = true;
         this.contentLabel.Font = new System.Drawing.Font("Segoe UI", 9F);
         this.contentLabel.Location = new System.Drawing.Point(13, 24);
         this.contentLabel.Name = "contentLabel";
         this.contentLabel.Size = new System.Drawing.Size(38, 15);
         this.contentLabel.TabIndex = 0;
         this.contentLabel.Text = "label1";
         // 
         // bottomPanel
         // 
         this.bottomPanel.AutoSize = true;
         this.bottomPanel.BackColor = System.Drawing.SystemColors.ControlLight;
         this.bottomPanel.ColumnCount = 2;
         this.bottomPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
         this.bottomPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
         this.bottomPanel.Controls.Add(this.buttonPanel, 1, 1);
         this.bottomPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
         this.bottomPanel.Location = new System.Drawing.Point(0, 64);
         this.bottomPanel.MinimumSize = new System.Drawing.Size(275, 42);
         this.bottomPanel.Name = "bottomPanel";
         this.bottomPanel.RightToLeft = System.Windows.Forms.RightToLeft.No;
         this.bottomPanel.RowCount = 3;
         this.bottomPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
         this.bottomPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
         this.bottomPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
         this.bottomPanel.Size = new System.Drawing.Size(284, 42);
         this.bottomPanel.TabIndex = 1;
         // 
         // CustomMsgBox
         // 
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
         this.AutoSize = true;
         this.ClientSize = new System.Drawing.Size(284, 106);
         this.Controls.Add(this.mainPanel);
         this.Controls.Add(this.bottomPanel);
         this.MaximizeBox = false;
         this.MaximumSize = new System.Drawing.Size(650, 400);
         this.MinimizeBox = false;
         this.Name = "CustomMsgBox";
         this.ShowIcon = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
         this.Text = "CustomMsgBox";
         this.TopMost = true;
         this.buttonPanel.ResumeLayout(false);
         this.buttonPanel.PerformLayout();
         this.mainPanel.ResumeLayout(false);
         this.mainPanel.PerformLayout();
         this.bottomPanel.ResumeLayout(false);
         this.bottomPanel.PerformLayout();
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      #endregion

      public System.Windows.Forms.FlowLayoutPanel buttonPanel;
      private System.Windows.Forms.TableLayoutPanel mainPanel;
      public System.Windows.Forms.Label contentLabel;
      private System.Windows.Forms.Button buttonYes;
      private System.Windows.Forms.Button buttonNo;
      private System.Windows.Forms.Button buttonCancel;
      private System.Windows.Forms.TableLayoutPanel bottomPanel;
   }
}

Example calls:

DialogResult dr;
         dr = CustomMsgBox.Show("content", "caption", MessageBoxButtons.YesNoCancel);
         dr = CustomMsgBox.Show("content", "caption", MessageBoxButtons.OK);
         dr = CustomMsgBox.Show("content", "caption", "Button1");
         dr = CustomMsgBox.Show("content", "caption", "Button1", "Button2");
         dr = CustomMsgBox.Show("content", "caption", "Button1", "Button2", "Button3");

Picture of the CustomMessageBox

Upvotes: 0

Greg Kowieski
Greg Kowieski

Reputation: 129

This may not be the prettiest, but if you don't want to use the MessageBoxManager, (which is awesome):

 public static DialogResult DialogBox(string title, string promptText, ref string value, string button1 = "OK", string button2 = "Cancel", string button3 = null)
    {
        Form form = new Form();
        Label label = new Label();
        TextBox textBox = new TextBox();
        Button button_1 = new Button();
        Button button_2 = new Button();
        Button button_3 = new Button();

        int buttonStartPos = 228; //Standard two button position


        if (button3 != null)
            buttonStartPos = 228 - 81;
        else
        {
            button_3.Visible = false;
            button_3.Enabled = false;
        }


        form.Text = title;

        // Label
        label.Text = promptText;
        label.SetBounds(9, 20, 372, 13);
        label.Font = new Font("Microsoft Tai Le", 10, FontStyle.Regular);

        // TextBox
        if (value == null)
        {
        }
        else
        {
            textBox.Text = value;
            textBox.SetBounds(12, 36, 372, 20);
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
        }

        button_1.Text = button1;
        button_2.Text = button2;
        button_3.Text = button3 ?? string.Empty;
        button_1.DialogResult = DialogResult.OK;
        button_2.DialogResult = DialogResult.Cancel;
        button_3.DialogResult = DialogResult.Yes;


        button_1.SetBounds(buttonStartPos, 72, 75, 23);
        button_2.SetBounds(buttonStartPos + 81, 72, 75, 23);
        button_3.SetBounds(buttonStartPos + (2 * 81), 72, 75, 23);

        label.AutoSize = true;
        button_1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        button_2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        button_3.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(396, 107);
        form.Controls.AddRange(new Control[] { label, button_1, button_2 });
        if (button3 != null)
            form.Controls.Add(button_3);
        if (value != null)
            form.Controls.Add(textBox);

        form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = button_1;
        form.CancelButton = button_2;

        DialogResult dialogResult = form.ShowDialog();
        value = textBox.Text;
        return dialogResult;
    }

Upvotes: 9

BornToCode
BornToCode

Reputation: 10233

I didn't think it would be that simple! go to this link: https://www.codeproject.com/Articles/18399/Localizing-System-MessageBox

Download the source. Take the MessageBoxManager.cs file, add it to your project. Now just register it once in your code (for example in the Main() method inside your Program.cs file) and it will work every time you call MessageBox.Show():

    MessageBoxManager.OK = "Alright";
    MessageBoxManager.Yes = "Yep!";
    MessageBoxManager.No = "Nope";
    MessageBoxManager.Register();

See this answer for the source code here for MessageBoxManager.cs.

Upvotes: 109

user3256312
user3256312

Reputation: 341

Here is the content of the file MessageBoxManager.cs

#pragma warning disable 0618

using System;

using System.Text;

using System.Runtime.InteropServices;

using System.Security.Permissions;

[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]

namespace System.Windows.Forms

{

    public class MessageBoxManager
    {
        private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        private delegate bool EnumChildProc(IntPtr hWnd, IntPtr lParam);

        private const int WH_CALLWNDPROCRET = 12;
        private const int WM_DESTROY = 0x0002;
        private const int WM_INITDIALOG = 0x0110;
        private const int WM_TIMER = 0x0113;
        private const int WM_USER = 0x400;
        private const int DM_GETDEFID = WM_USER + 0;

        private const int MBOK = 1;
        private const int MBCancel = 2;
        private const int MBAbort = 3;
        private const int MBRetry = 4;
        private const int MBIgnore = 5;
        private const int MBYes = 6;
        private const int MBNo = 7;


        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(IntPtr idHook);

        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "GetWindowTextLengthW", CharSet = CharSet.Unicode)]
        private static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);

        [DllImport("user32.dll")]
        private static extern int EndDialog(IntPtr hDlg, IntPtr nResult);

        [DllImport("user32.dll")]
        private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        private static extern int GetDlgCtrlID(IntPtr hwndCtl);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

        [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]
        private static extern bool SetWindowText(IntPtr hWnd, string lpString);


        [StructLayout(LayoutKind.Sequential)]
        public struct CWPRETSTRUCT
        {
            public IntPtr lResult;
            public IntPtr lParam;
            public IntPtr wParam;
            public uint   message;
            public IntPtr hwnd;
        };

        private static HookProc hookProc;
        private static EnumChildProc enumProc;
        [ThreadStatic]
        private static IntPtr hHook;
        [ThreadStatic]
        private static int nButton;

        /// <summary>
        /// OK text
        /// </summary>
        public static string OK = "&OK";
        /// <summary>
        /// Cancel text
        /// </summary>
        public static string Cancel = "&Cancel";
        /// <summary>
        /// Abort text
        /// </summary>
        public static string Abort = "&Abort";
        /// <summary>
        /// Retry text
        /// </summary>
        public static string Retry = "&Retry";
        /// <summary>
        /// Ignore text
        /// </summary>
        public static string Ignore = "&Ignore";
        /// <summary>
        /// Yes text
        /// </summary>
        public static string Yes = "&Yes";
        /// <summary>
        /// No text
        /// </summary>
        public static string No = "&No";

        static MessageBoxManager()
        {
            hookProc = new HookProc(MessageBoxHookProc);
            enumProc = new EnumChildProc(MessageBoxEnumProc);
            hHook = IntPtr.Zero;
        }

        /// <summary>
        /// Enables MessageBoxManager functionality
        /// </summary>
        /// <remarks>
        /// MessageBoxManager functionality is enabled on current thread only.
        /// Each thread that needs MessageBoxManager functionality has to call this method.
        /// </remarks>
        public static void Register()
        {
            if (hHook != IntPtr.Zero)
                throw new NotSupportedException("One hook per thread allowed.");
            hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
        }

        /// <summary>
        /// Disables MessageBoxManager functionality
        /// </summary>
        /// <remarks>
        /// Disables MessageBoxManager functionality on current thread only.
        /// </remarks>
        public static void Unregister()
        {
            if (hHook != IntPtr.Zero)
            {
                UnhookWindowsHookEx(hHook);
                hHook = IntPtr.Zero;
            }
        }

        private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
                return CallNextHookEx(hHook, nCode, wParam, lParam);

            CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
            IntPtr hook = hHook;

            if (msg.message == WM_INITDIALOG)
            {
                int nLength = GetWindowTextLength(msg.hwnd);
                StringBuilder className = new StringBuilder(10);
                GetClassName(msg.hwnd, className, className.Capacity);
                if (className.ToString() == "#32770")
                {
                    nButton = 0;
                    EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero);
                    if (nButton == 1)
                    {
                        IntPtr hButton = GetDlgItem(msg.hwnd, MBCancel);
                        if (hButton != IntPtr.Zero)
                            SetWindowText(hButton, OK);
                    }
                }
            }

            return CallNextHookEx(hook, nCode, wParam, lParam);
        }

        private static bool MessageBoxEnumProc(IntPtr hWnd, IntPtr lParam)
        {
            StringBuilder className = new StringBuilder(10);
            GetClassName(hWnd, className, className.Capacity);
            if (className.ToString() == "Button")
            {
                int ctlId = GetDlgCtrlID(hWnd);
                switch (ctlId)
                {
                    case MBOK:
                        SetWindowText(hWnd, OK);
                        break;
                    case MBCancel:
                        SetWindowText(hWnd, Cancel);
                        break;
                    case MBAbort:
                        SetWindowText(hWnd, Abort);
                        break;
                    case MBRetry:
                        SetWindowText(hWnd, Retry);
                        break;
                    case MBIgnore:
                        SetWindowText(hWnd, Ignore);
                        break;
                    case MBYes:
                        SetWindowText(hWnd, Yes);
                        break;
                    case MBNo:
                        SetWindowText(hWnd, No);
                        break;

                }
                nButton++;
            }

            return true;
        }


    }
}

Upvotes: 34

kbvishnu
kbvishnu

Reputation: 15650

Just add a new form and add buttons and a label. Give the value to be shown and the text of the button, etc. in its constructor, and call it from anywhere you want in the project.

In project -> Add Component -> Windows Form and select a form

Add some label and buttons.

Initialize the value in constructor and call it from anywhere.

public class form1:System.Windows.Forms.Form
{
    public form1()
    {
    }

    public form1(string message,string buttonText1,string buttonText2)
    {
       lblMessage.Text = message;
       button1.Text = buttonText1;
       button2.Text = buttonText2;
    }
}

// Write code for button1 and button2 's click event in order to call 
// from any where in your current project.

// Calling

Form1 frm = new Form1("message to show", "buttontext1", "buttontext2");
frm.ShowDialog();

Upvotes: 40

Related Questions