2012-01-15 74 views
1

基於我的研究,在父窗體上獲取MessageBox窗體居中的唯一方法是編寫自定義MessageBox類。我已經成功實現了CustomMessageBox表單,並能夠將我的錯誤和信息性消息集中在父表單上。但是,我無法弄清楚如何使CustomMessageBox表單爲靜態,以便我不必實例化新的CustomMessageBox表單。我希望能夠只是調用靜態方法如下圖所示:錯誤和信息的自定義消息框

CustomMessageBox.Show(類型,郵件等)

我MessageBox類的基本版本如下。理想情況下,我希望具有顯示此表單的功能,而不必實例化我的CustomMessageForm。這可能嗎?

namespace MarineService 
{ 
    public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm 
    { 
     private static CustomMessageForm form = new CustomMessageForm(); 

     public CustomMessageForm() 
     { 
      InitializeComponent(); 
     } 

     public void ShowDialog(string type, string message) 
     { 
      this.Text = type + "Information"; 
      this.groupMessage.Text = type + "Information"; 
      this.memoEditMessage.Lines[0] = message; 

     } 
    } 
} 
+0

當我在CustomMessageBox上工作時,想到的一個想法是創建一個靜態實用程序類,該靜態實用程序類上有一個靜態方法,它創建了我的CustomMessageBox的一個新實例,然後顯示它。但是,如果實現此解決方案,則必須將父窗體傳遞到實用程序類的靜態方法中,以將新CustomMessageBox居中放在父窗口上。我不確定這是否是好設計,因爲我是Windows編程的新手。歡迎任何意見和/或反饋。 – Grasshopper 2012-01-15 13:27:15

+2

[Winforms的可能的重複 - 如何使MessageBox出現在MainForm中心?](http://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform ) – 2012-01-15 13:29:53

回答

2

事情是這樣的:

public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm 
{ 
     private static CustomMessageForm form = new CustomMessageForm(); 

     public CustomMessageForm() 
     { 
      InitializeComponent(); 
     } 

     private void ShowDialog(string type, string message) 
     { 
      form .Text = type + "Information"; 
      form .groupMessage.Text = type + "Information"; 
      form .memoEditMessage.Lines[0] = message; 
      form.ShowDialog(); 

     } 

     public static Show(string type, string message) 
     { 
      if(form.Visible) 
       form.Close(); 
      ShowDialog(type, message); 
     } 
} 

,並使用此類似:

CustomMessageForm.Show("customtype", "warning!"); 

類似的東西,只是一個想法。

如果這不是你要求的,請澄清。

+0

謝謝,這正是我所需要的。我閱讀漢斯的帖子,但是,它沒有點擊我只能讓Show方法靜態,而不讓整個類都靜態,直到我閱讀你的文章。感謝幫助。 – Grasshopper 2012-01-15 13:40:53