0

I'm having trouble making a dialog with MessageBox with a text like "Do you want to save chanes to Untitled?" with 3 buttons like "save","don't save" and "cancel" ?

    private void MenuItemNew()
    {
        if (textBox.Text == "")
        {
            textBox.Text = String.Empty;
        }
        else
            DialogResult result3 = MessageBox.Show("Do you want to save changes to Untitled?",
"The Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);

    if (result3 == DialogResult.Yes)
    {
        //statements if Result = Yes
    }
    else if (result3 == DialogResult.No)
    {
        //statements if Result = NO

}

i tried this but its not working

Melody Hajian
  • 275
  • 1
  • 3
  • 12

2 Answers2

1

Use this sample:

  MessageBox.Show("Dot Net Perls is awesome.");
        //
        // Dialog box with text and a title. [2]
        //
        MessageBox.Show("Dot Net Perls is awesome.",
        "Important Message");
        //
        // Dialog box with two buttons: yes and no. [3]
        //
        DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
        "Important Question",
        MessageBoxButtons.YesNo);
        //
        // Dialog box with question icon. [4]
        //
        DialogResult result2 = MessageBox.Show("Is Dot Net Perls awesome?",
        "Important Query",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
        //
        // Dialog box with question icon and default button. [5]
        //
        DialogResult result3 = MessageBox.Show("Is Visual Basic awesome?",
        "The Question",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question,
        MessageBoxDefaultButton.Button2);
  • 2
    @MelodyHajian I think if this code is not useful for you you should create custom message box like this http://www.codeproject.com/Articles/17253/A-Custom-Message-Box –  Nov 16 '13 at 08:42
  • 1
    whats the problem with my codes? Error for DialogResult Embedded statement cannot be a declaration or labeled statement – Melody Hajian Nov 16 '13 at 08:49
  • @Sudhakar Error for DialogResult Embedded statement cannot be a declaration or labeled statement – Melody Hajian Nov 16 '13 at 08:56
  • it looks like the buttons can be just Yes,No or Cancel :| but i need them to be Save,Don't Save and Cancel – Melody Hajian Nov 16 '13 at 08:57
0

your Question is not clear, if you want to display a MessageBox

Try this:

DialogResult result3 = MessageBox.Show("Do you want to save changes to Untitled?",
    "The Question",
    MessageBoxButtons.YesNoCancel,
    MessageBoxIcon.Question);

//While handling The Result

        if (result3 == DialogResult.Yes)
        {
            //statements if Result = Yes
        }
        else if (result3 == DialogResult.No)
        {
            //statements if Result = NO
        }

Solution:

private void MenuItemNew()
        {
            if (textBox.Text.ToString().Trim().Equals(""))
            {
                textBox.Text = String.Empty;
            }
            else
            {
DialogResult result3 = MessageBox.Show("Do you want to save changes to Untitled?","The Question",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);

        if (result3 == DialogResult.Yes)
        {
            //statements if Result = Yes
        }
        else if (result3 == DialogResult.No)
        {
            //statements if Result = NO
        }

        }//end of else block
     }//end of function

if you want to create a MessageBox with your own Buttons , then you have to design one.

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67