2011-12-28 135 views
17

剛纔我注意到,當一個屬性設置爲無效值時,Visual Studio會顯示一個消息框,其中包含詳細信息。例如:如何在WinForms中顯示帶有詳細信息的消息框?

是否有可能作出的WinForms這種類型的消息框?

我曾嘗試下面的代碼:

MessageBox.Show("Error in Division Fill.\n" + ex.Message, 
       "Information",    
       MessageBoxButtons.OK, 
       MessageBoxIcon.Information, 
       MessageBoxOptions.RightAlign); 

但是,這將產生以下錯誤:

Error 24 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string, string, System.Windows.Forms.MessageBoxButtons, System.Windows.Forms.MessageBoxIcon, System.Windows.Forms.MessageBoxDefaultButton)' has some invalid arguments

G:\Jagadeeswaran\Nov 17\MCS-SPS School\MCS-SPS School\Certificate\Transfer.cs 164 21 MCS-SPS School

我怎樣才能解決這個錯誤,並得到一個消息框,顯示額外的細節?

+6

這自定義對話框;你不能通過使用標準的'MessageBox.Show'重載之一來得到它。 – 2011-12-28 08:17:00

+0

謝謝。那麼MessageBoxOptions在那裏有什麼用處。 – Sagotharan 2011-12-28 09:23:09

+1

'MessageBoxOptions'記錄在[這裏](http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxoptions.aspx)。我不確定你爲什麼認爲'RightAlign'與顯示「Details」有什麼關係。它只是簡單地使消息框中的文本與右側對齊,就像在RTL系統上一樣。 – 2011-12-28 09:24:48

回答

24

正如其他人所指出的,您應該編寫一個具有所需功能的自定義對話框。有關此方面的幫助,可以查看PropertyGrid對於此對話框(可能是反編譯器)使用的實際實現,它是.NET 4.0的System.Windows.Forms.PropertyGridInternal.GridErrorDlg類型,它位於System.Windows.Forms程序集的內部。

真的不會推薦它(可能在未來的版本中打破),但如果你感覺真的很懶,你可以直接使用這種內部類型使用反射。

// Get reference to the dialog type. 
var dialogTypeName = "System.Windows.Forms.PropertyGridInternal.GridErrorDlg"; 
var dialogType = typeof(Form).Assembly.GetType(dialogTypeName); 

// Create dialog instance. 
var dialog = (Form)Activator.CreateInstance(dialogType, new PropertyGrid()); 

// Populate relevant properties on the dialog instance. 
dialog.Text = "Sample Title"; 
dialogType.GetProperty("Details").SetValue(dialog, "Sample Details", null); 
dialogType.GetProperty("Message").SetValue(dialog, "Sample Message", null); 

// Display dialog. 
var result = dialog.ShowDialog(); 

結果

Details Dialog

+1

如果我不想在這個消息框中取消選項,我該怎麼辦? – 2013-08-26 18:52:07

10

您需要設置Form的以下屬性來創建自定義對話框/消息窗口。

  1. 的AcceptButton
  2. CancelButton
  3. FormBorderStyle = FixedDialog
  4. MaximizeBox =假
  5. MinimizeBox =假
  6. ShowIcon =假
  7. ShowInTaskBar =假
  8. 中StartPosition = CenterParent

現在,使用ShowDialog()方法來顯示自定義對話框。

MyDialog dialog=new MyDialog(); 
DialogResult result=dialog.ShowDialog(); 
if(result == DialogResult.OK) 
{ 
    // 
} 

有關對話框的詳細信息,請閱讀MSDN文章 - Dialog Boxes (Visual C#)

+0

基於這個答案,我寫了自定義表單。看看這裏:http://stackoverflow.com/a/40469355/3314922 – gneri 2016-11-15 22:18:28

3

只寫自己的對話框,沒有超載像你想展示方法。

0

你可以簡單地這樣做:

catch (Exception error) 
{ 
    throw new Exception("Error with details button! \n"+error); 
} 

的文本 「」 是 「Adicionar Fotografia」。

注意:在編輯器中運行時運行應用程序(.exe)時,此代碼正常工作,它不起作用並且有意義。

This is an example of the code above

+0

雖然此代碼可能會回答問題,但提供有關此代碼爲何和/或如何回答問題的其他上下文可提高其長期價值。 – ryanyuyu 2016-03-21 13:01:37

1

基於@ AVD的答案上面(我upvoted)我寫了以下內容。將它粘貼到你使用VS模板生成的表單中,它應該可以工作,也許只需要很少的調整。

我的代碼:

using System; 
using System.Windows.Forms; 

namespace MessageBoxWithDetails 
{ 
    /// <summary> 
    /// A dialog-style form with optional colapsable details section 
    /// </summary> 
    public partial class MessageBoxWithDetails : Form 
    { 
     private const string DetailsFormat = "Details {0}"; 

    public MessageBoxWithDetails(string message, string title, string details = null) 
    { 
     InitializeComponent(); 

     lblMessage.Text = message; 
     this.Text = title; 

     if (details != null) 
     { 
      btnDetails.Enabled = true; 
      btnDetails.Text = DownArrow; 
      tbDetails.Text = details; 
     } 
    } 

    private string UpArrow 
    { 
     get 
     { 
      return string.Format(DetailsFormat, char.ConvertFromUtf32(0x25B4)); 
     } 
    } 

    private string DownArrow 
    { 
     get 
     { 
      return string.Format(DetailsFormat, char.ConvertFromUtf32(0x25BE)); 
     } 
    } 

    /// <summary> 
    /// Meant to give the look and feel of a regular MessageBox 
    /// </summary> 
    public static void Show(string message, string title, string details = null) 
    { 
     new MessageBoxWithDetails(message, title, details).ShowDialog(); 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     // Change these properties now so the label is rendered so we get its real height 
     var height = lblMessage.Height; 
     SetMessageBoxHeight(height); 
    } 

    private void SetMessageBoxHeight(int heightChange) 
    { 
     this.Height = this.Height + heightChange; 
     if (this.Height < 150) 
     { 
      this.Height = 150; 
     } 
    } 

    private void btnClose_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void btnDetails_Click(object sender, EventArgs e) 
    { 
     // Re-anchoring the controls so they stay in their place while the form is resized 
     btnCopy.Anchor = AnchorStyles.Top; 
     btnClose.Anchor = AnchorStyles.Top; 
     btnDetails.Anchor = AnchorStyles.Top; 
     tbDetails.Anchor = AnchorStyles.Top; 

     tbDetails.Visible = !tbDetails.Visible; 
     btnCopy.Visible = !btnCopy.Visible; 

     btnDetails.Text = tbDetails.Visible ? UpArrow : DownArrow; 

     SetMessageBoxHeight(tbDetails.Visible ? tbDetails.Height + 10 : -tbDetails.Height - 10); 
    } 

    private void btnCopy_Click(object sender, EventArgs e) 
    { 
     Clipboard.SetText(tbDetails.Text); 
    } 
} 

設計自動生成的代碼(它應該給你就放什麼形式,如果你不想粘貼的想法):

namespace MessageBoxWithDetails 
{ 
    partial class MessageBoxWithDetails 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.btnClose = new System.Windows.Forms.Button(); 
      this.btnDetails = new System.Windows.Forms.Button(); 
      this.btnCopy = new System.Windows.Forms.Button(); 
      this.lblMessage = new System.Windows.Forms.Label(); 
      this.tbDetails = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // btnClose 
      // 
      this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right))); 
      this.btnClose.Location = new System.Drawing.Point(267, 37); 
      this.btnClose.Name = "btnClose"; 
      this.btnClose.Size = new System.Drawing.Size(75, 23); 
      this.btnClose.TabIndex = 0; 
      this.btnClose.Text = "Close"; 
      this.btnClose.UseVisualStyleBackColor = true; 
      this.btnClose.Click += new System.EventHandler(this.btnClose_Click); 
      // 
      // btnDetails 
      // 
      this.btnDetails.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right))); 
      this.btnDetails.Enabled = false; 
      this.btnDetails.Location = new System.Drawing.Point(12, 37); 
      this.btnDetails.Name = "btnDetails"; 
      this.btnDetails.Size = new System.Drawing.Size(75, 23); 
      this.btnDetails.TabIndex = 1; 
      this.btnDetails.Text = "Details"; 
      this.btnDetails.UseVisualStyleBackColor = true; 
      this.btnDetails.Click += new System.EventHandler(this.btnDetails_Click); 
      // 
      // btnCopy 
      // 
      this.btnCopy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right))); 
      this.btnCopy.Location = new System.Drawing.Point(93, 37); 
      this.btnCopy.Name = "btnCopy"; 
      this.btnCopy.Size = new System.Drawing.Size(102, 23); 
      this.btnCopy.TabIndex = 4; 
      this.btnCopy.Text = "Copy To Clipboard"; 
      this.btnCopy.UseVisualStyleBackColor = true; 
      this.btnCopy.Visible = false; 
      this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click); 
      // 
      // lblMessage 
      // 
      this.lblMessage.AutoSize = true; 
      this.lblMessage.Location = new System.Drawing.Point(12, 9); 
      this.lblMessage.MaximumSize = new System.Drawing.Size(310, 0); 
      this.lblMessage.Name = "lblMessage"; 
      this.lblMessage.Size = new System.Drawing.Size(35, 13); 
      this.lblMessage.TabIndex = 5; 
      this.lblMessage.Text = "label1"; 
      // 
      // tbDetails 
      // 
      this.tbDetails.Anchor = System.Windows.Forms.AnchorStyles.Bottom; 
      this.tbDetails.Location = new System.Drawing.Point(12, 68); 
      this.tbDetails.MaximumSize = new System.Drawing.Size(328, 100); 
      this.tbDetails.Multiline = true; 
      this.tbDetails.Name = "tbDetails"; 
      this.tbDetails.ReadOnly = true; 
      this.tbDetails.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 
      this.tbDetails.Size = new System.Drawing.Size(328, 100); 
      this.tbDetails.TabIndex = 6; 
      this.tbDetails.Visible = false; 
      // 
      // MessageBoxWithDetails 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(354, 72); 
      this.Controls.Add(this.tbDetails); 
      this.Controls.Add(this.lblMessage); 
      this.Controls.Add(this.btnCopy); 
      this.Controls.Add(this.btnDetails); 
      this.Controls.Add(this.btnClose); 
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
      this.MaximizeBox = false; 
      this.MinimizeBox = false; 
      this.Name = "MessageBoxWithDetails"; 
      this.ShowIcon = false; 
      this.ShowInTaskbar = false; 
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.Button btnClose; 
     private System.Windows.Forms.Button btnDetails; 
     private System.Windows.Forms.Button btnCopy; 
     private System.Windows.Forms.Label lblMessage; 
     private System.Windows.Forms.TextBox tbDetails; 
    } 
}