C# 如何在WinForms中显示包含详细信息的消息框?

C# 如何在WinForms中显示包含详细信息的消息框?,c#,.net,winforms,messagebox,C#,.net,Winforms,Messagebox,刚才我注意到,当属性设置为无效值时,VisualStudio会显示一个包含详细信息的消息框。例如: 是否可以在WinForms中创建这种类型的消息框 我尝试了以下代码: MessageBox.Show("Error in Division Fill.\n" + ex.Message, "Information", MessageBoxButtons.OK,

刚才我注意到,当属性设置为无效值时,VisualStudio会显示一个包含详细信息的消息框。例如:

是否可以在WinForms中创建这种类型的消息框

我尝试了以下代码:

MessageBox.Show("Error in Division Fill.\n" + ex.Message,
                "Information",            
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxOptions.RightAlign);
但这产生了以下错误:

错误24“System.Windows.Forms.MessageBox.Show(string,string,System.Windows.Forms.MessageBoxButtons,System.Windows.Forms.MessageBoxIcon,System.Windows.Forms.MessageBoxDefaultButton)”的最佳重载方法匹配具有一些无效参数

G:\Jagadeeswaran\11月17日\MCS-SPS学校\MCS-SPS学校\Certificate\Transfer.cs 164 21 MCS-SPS学校


如何修复此错误并获得显示其他详细信息的消息框?

您需要设置的以下属性以创建自定义对话框/消息窗口

  • 接受按钮
  • 取消按钮
  • FormBorderStyle=FixedDialog
  • 最大化ebox=False
  • b x=False
  • ShowIcon=False
  • ShowInTaskBar=False
  • StartPosition=CenterParent
  • 现在,使用方法显示自定义对话框

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

    有关Dialog read MSDN article的更多信息-

    只需编写您自己的对话框,就不会像您希望显示的方法那样出现重载。

    正如其他人所指出的,您应该编写一个具有所需功能的自定义对话框。有关这方面的帮助,您可以查看
    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();
    
    结果


    您只需执行以下操作:

    catch (Exception error)
    {
        throw new Exception("Error with details button! \n"+error);
    }
    
    “”中的文本为“Adicionar Fotografia”

    注意:当在编辑器中运行应用程序(.exe)时,此代码可以正常工作。它不工作,并且有意义

    这是上述代码的一个示例:


    我也遇到了同样的问题。我建议您使用windowsAPIcode包。 这个网站将是一个很大的帮助。只要正确地按照说明操作即可:

    基于(我投了赞成票)我写了以下内容。将它粘贴到您使用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);
        }
    }
    
    使用系统;
    使用System.Windows.Forms;
    命名空间MessageBoxWithDetails
    {
    /// 
    ///带有可选colapsable details部分的对话框样式窗体
    /// 
    公共部分类MessageBoxWithDetails:表单
    {
    private const string DetailsFormat=“Details{0}”;
    public MessageBoxWithDetails(字符串消息、字符串标题、字符串详细信息=null)
    {
    初始化组件();
    lblMessage.Text=消息;
    这个文本=标题;
    如果(详细信息!=null)
    {
    btnDetails.Enabled=true;
    btnDetails.Text=向下箭头;
    tbDetails.Text=详细信息;
    }
    }
    私有字符串向上箭头
    {
    得到
    {
    返回string.Format(DetailsFormat,char.ConvertFromUtf32(0x25B4));
    }
    }
    私有字符串向下箭头
    {
    得到
    {
    返回string.Format(DetailsFormat,char.ConvertFromUtf32(0x25BE));
    }
    }
    /// 
    ///旨在提供常规MessageBox的外观和感觉
    /// 
    公共静态无效显示(字符串消息、字符串标题、字符串详细信息=null)
    {
    新建MessageBox WithDetails(消息、标题、详细信息)。ShowDialog();
    }
    受保护的覆盖无效加载(事件参数e)
    {
    基础荷载(e);
    //现在更改这些属性,以便渲染标签,从而获得其真实高度
    变量高度=lbl消息高度;
    设置MessageBoxHeight(高度);
    }
    私有void SetMessageBoxHeight(内部高度更改)
    {
    this.Height=this.Height+heightChange;
    如果(此高度<150)
    {
    这个。高度=150;
    }
    }
    私有void btnClose_单击(对象发送者,事件参数e)
    {
    这个。关闭();
    }
    私有void btnDetails\u单击(对象发送者,事件参数e)
    {
    //重新锚定控件,使其在调整窗体大小时保持原位
    btnCopy.Anchor=锚点样式.Top;
    btnClose.Anchor=AnchorStyles.Top;
    btnDetails.Anchor=锚点样式.Top;
    tbDetails.Anchor=AnchorStyles.Top;
    tbDetails.Visible=!tbDetails.Visible;
    可见=!btnCopy.Visible;
    btnDetails.Text=tbDetails.Visible?向上箭头:向下箭头;
    SetMessageBoxHeight(tbDetails.Visible?tbDetails.Height+10:-tbDetails.Height-10);
    }
    私有void btnCopy\u单击(对象发送方,事件参数e)
    {
    剪贴板.SetText(tbDetails.Text);
    }
    }
    
    Designer自动生成的代码(如果您不想粘贴表单,它应该让您知道在表单中放置什么):

    namespace MessageBoxWithDetails
    {
    部分类MessageBoxWithDetails
    {
    /// 
    ///必需的设计器变量。
    /// 
    private System.ComponentModel.IContainer components=null;
    /// 
    ///清理所有正在使用的资源。
    /// 
    ///如果应释放托管资源,则为true;否则为false。
    受保护的覆盖无效处置(布尔处置)
    {
    if(处理和(组件!=null))
    {
    组件。Dispose();
    }
    基地。处置(处置);
    }
    #区域Windows窗体设计器生成的代码
    /// 
    ///设计器支持所需的方法-不修改
    ///此方法的内容与代码编辑器一起使用。
    /// 
    私有void InitializeComponent()
    {
    this.btnClose=新系统.Win
    
    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;
        }
    }