如何更改MessageBox按钮上的文本?

8
这是我所使用的代码:
MessageBox.Show("Do you want to save changes..?", "Save",
    MessageBoxButtons.YesNoCancel);

我想改变消息框按钮上的文字,这是否可能?


1
http://www.codeproject.com/Articles/18399/Localizing-System-MessageBox - alexn
1
也许这会对你有所帮助:http://www.codeproject.com/Articles/18399/Localizing-System-MessageBox - SergioMSCosta
2个回答

8
据我所知,无法更改MessageBox弹出窗口的默认文本。
对您最简单的做法是创建一个简单的表单,包含一个标签和几个按钮。下面是一个简单的示例,您可以将其放入您的代码中,并按照您的意愿自定义表单。
public class CustomMessageBox:System.Windows.Forms.Form
{
    Label message = new Label();
    Button b1 = new Button();
    Button b2 = new Button();

    public CustomMessageBox()
    {

    }

    public CustomMessageBox(string title, string body, string button1, string button2)
    {
        this.ClientSize = new System.Drawing.Size(490, 150);
        this.Text = title;

        b1.Location = new System.Drawing.Point(411, 112);
        b1.Size = new System.Drawing.Size(75, 23);
        b1.Text = button1;
        b1.BackColor = Control.DefaultBackColor;

        b2.Location = new System.Drawing.Point(311, 112);
        b2.Size = new System.Drawing.Size(75, 23);
        b2.Text = button2; 
        b2.BackColor = Control.DefaultBackColor;

        message.Location = new System.Drawing.Point(10, 10);
        message.Text = body;
        message.Font = Control.DefaultFont;
        message.AutoSize = true;

        this.BackColor = Color.White;
        this.ShowIcon = false;

        this.Controls.Add(b1);
        this.Controls.Add(b2);
        this.Controls.Add(message);
    }        
}

您可以随时从任何需要的地方调用它,如下所示:

        CustomMessageBox customMessage = new CustomMessageBox(
            "Warning",
            "Are you sure you want to exit without saving?",
            "Yeah Sure!",
            "No Way!" 
            );
        customMessage.StartPosition = FormStartPosition.CenterParent;
        customMessage.ShowDialog();

0

我认为MessageBox是一个Win32 API的怪物,这意味着它在.NET的范围之外。因此,它对自定义/本地化毫不知情。所以你需要像James Miller建议的那样自己编写消息框。

为什么微软决定不在Forms中放置一个.NET-enabled的消息框超出了我的理解...


1
任务对话框旨在取代消息框。 - Dialecticus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接