【问题标题】:making a dialog with messagebox c#用messagebox c#创建一个对话框
【发布时间】:2013-11-16 08:10:53
【问题描述】:

我在与 MessageBox 进行对话时遇到了问题,其中包含“您想将通道保存到 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

}

我试过了,但它不起作用

【问题讨论】:

标签: c# dialog messagebox


【解决方案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);

【讨论】:

  • @MelodyHajian 我认为如果这段代码对你没有用,你应该创建像这样的自定义消息框codeproject.com/Articles/17253/A-Custom-Message-Box
  • 我的代码有什么问题? DialogResult Embedded 语句的错误不能是声明或标记语句
  • @Sudhakar Error for DialogResult Embedded 语句不能是声明或标记语句
  • 看起来按钮可以只是是、否或取消:|但我需要他们保存,不要保存和取消
【解决方案2】:

你的问题不清楚,如果你想显示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

如果你想用你自己的 Buttons 创建一个MessageBox,那么你必须设计一个。

【讨论】:

  • 我该如何改进呢?你能解释一下这有什么问题吗?
  • 我说 DialogResult 错误:嵌入式语句不能是声明或标记语句
  • 对不起,我没有得到你,你在哪里得到这个错误?上面的代码没有问题。
  • 这可能是由于缺少一些报价或某些原因,请分享您的代码。
  • 用解决方案部分检查我的答案,效果很好
猜你喜欢
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多