2011-12-28 24 views
17

Chỉ cần bây giờ tôi nhận thấy rằng Visual Studio hiển thị một hộp thông báo với các chi tiết khi một thuộc tính được đặt thành một giá trị không hợp lệ. Ví dụ:Làm cách nào để hiển thị hộp thư có chi tiết trong WinForms?

Có thể làm cho loại hộp thông báo trong WinForms?

Tôi đã thử đoạn mã sau:

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

Nhưng điều này tạo ra các lỗi sau:

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

Làm thế nào tôi có thể khắc phục lỗi này và nhận được một hộp thông báo cho thấy thêm chi tiết?

+6

Đó là một hộp thoại tùy chỉnh; bạn không thể nhận được nó bằng cách sử dụng một trong những quá tải tiêu chuẩn 'MessageBox.Show'. –

+0

Cảm ơn. sau đó sử dụng MessageBoxOptions trong đó là gì. – Sagotharan

+1

Các 'MessageBoxOptions' được ghi lại [ở đây] (http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxoptions.aspx). Tôi không chắc tại sao bạn nghĩ 'RightAlign' có liên quan gì đến việc hiển thị "Details". Nó chỉ đơn giản là làm cho văn bản trong hộp tin nhắn được căn chỉnh bên phải, giống như trên một hệ thống RTL. –

Trả lời

24

Như những người khác đã chỉ ra, bạn nên viết một hộp thoại tùy chỉnh với các tính năng mong muốn. Để được trợ giúp về điều này, bạn có thể xem triển khai thực tế được sử dụng bởi PropertyGrid cho hộp thoại này (có lẽ với bộ giải mã), có nghĩa là .NET 4.0, loại System.Windows.Forms.PropertyGridInternal.GridErrorDlg, bên trong assembly System.Windows.Forms.

I thực sự sẽ không đề xuất (có thể phá vỡ trong bản phát hành trong tương lai), nhưng nếu bạn cảm thấy thực sự lười biếng, bạn có thể trực tiếp sử dụng loại nội bộ này bằng cách sử dụng sự phản chiếu.

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

quả:

Details Dialog

+1

nếu tôi không muốn hủy tùy chọn trong hộp thông báo này, tôi phải làm gì? –

10

Bạn cần đặt các thuộc tính sau đây của Form để tạo cửa sổ Hộp thoại/Tin nhắn tùy chỉnh.

  1. AcceptButton
  2. CancelButton
  3. FormBorderStyle = FixedDialog
  4. MaximizeBox = False
  5. MinimizeBox = False
  6. ShowIcon = False
  7. SHOWINTASKBAR = False
  8. StartPosition = CenterParent

Bây giờ, hãy sử dụng phương pháp ShowDialog() để hiển thị hộp thoại tùy chỉnh.

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

Để biết thêm thông tin về Dialog đọc MSDN bài viết - Dialog Boxes (Visual C#)

+0

Dựa trên câu trả lời này, tôi đã viết biểu mẫu tùy chỉnh. Hãy xem tại đây: http://stackoverflow.com/a/40469355/3314922 – gneri

3

chỉ cần viết thoại của riêng mình, không có tình trạng quá tải như bạn muốn hiển thị phương pháp.

0

Bạn chỉ có thể làm điều này:

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

Các văn bản trong "" là "Adicionar Fotografia".

Lưu ý: mã này hoạt động tốt khi chạy ứng dụng (.exe) trong khi chạy trong trình chỉnh sửa nó không hoạt động và có ý nghĩa.

This is an example of the code above

+0

Mặc dù mã này có thể trả lời câu hỏi, cung cấp ngữ cảnh bổ sung về lý do và/hoặc cách mã này trả lời câu hỏi cải thiện giá trị lâu dài của nó. – ryanyuyu

1

Dựa trên câu trả lời của @ AVD ở trên (tôi đã upvoted) tôi đã viết như sau. Dán mẫu này vào một biểu mẫu mà bạn đã tạo bằng mẫu VS và nó sẽ hoạt động, có thể với vài chỉnh sửa.

Mã của tôi:

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); 
    } 
} 

thiết kế tự động tạo mã (nó sẽ cho bạn một ý tưởng về những gì để đưa vào biểu mẫu nếu bạn không muốn để dán nó):

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; 
    } 
}