Search code examples
c#winformsmessagebox

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

Solution

  • 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();