2012-10-03 71 views
6

這是我的代碼,我使用...如何更改消息框按鈕上的文本?

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](http://www.codeproject.com/Articles/18399/Localizing-System-MessageBox) – SergioMSCosta

回答

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(); 
0

我想在MessageBox是一個Win32 API的野獸,這意味着它是.NET領域之外。因此它對定製/本地化沒有意義。所以你需要像詹姆斯米勒所建議的那樣推出自己的消息框。

爲什麼MS決定不在表單中啓用了.NET的消息框,把超出我...

+1

[任務對話框] (https://msdn.microsoft.com/en-us/library/windows/desktop/bb787471%28v=vs.85%29.aspx)是爲了取代消息框。 – Dialecticus

相關問題