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

时间:2012-10-03 07:44:26

标签: c# messagebox

这是我使用的代码......

 MessageBox.Show("Do you want to save changes..?", "Save", MessageBoxButtons.YesNoCancel);

我想更改消息框按钮上的文字是否可能.. ??

2 个答案:

答案 0 :(得分:7)

据我所知,无法更改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();

答案 1 :(得分:0)

我认为MessageBox是一个Win32 API野兽,这意味着它超出了.NET的范围。因此,它无视定制/本地化。所以你需要像James Miller建议的那样滚动你自己的消息框。

为什么MS决定不在.NET中使用支持.NET的消息框,这超出了我的范围......

相关问题