使用messagebox进行对话c#

时间:2013-11-16 08:10:54

标签: c# dialog messagebox

我在使用MessageBox进行对话时遇到问题,例如“你想将chanes保存到Untitled吗?”有3个按钮,如“保存”,“不保存”和“取消”?

    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

}

我尝试了这个,但它不起作用

2 个答案:

答案 0 :(得分:1)

使用此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);

答案 1 :(得分:0)

如果您想显示MessageBox

您的问题不明确

试试这个:

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

//处理结果时

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

<强>解决方案:

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

如果您想使用自己的按钮创建MessageBox,那么您必须设计一个。