簡體   English   中英

如何更改消息框按鈕上的文本?

[英]How to change text on Messagebox buttons?

這是我使用的代碼:

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

我想更改消息框按鈕上的文本是否可能?

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

我認為 MessageBox 是一個 Win32 API 野獸,這意味着它超出了 .NET 的范圍。 因此,它忽略了定制/本地化。 所以你需要像 James Miller 建議的那樣滾動你自己的消息框。

為什么 MS 決定不在 Forms 中放置支持 .NET 的消息框,這超出了我的理解......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM