C#中的Scrollable MessageBox

Kiq*_*net 4 c# scroll add-in messagebox

我在VS2008,C#中使用Addin,我需要显示消息(错误消息和其他).

我不知道消息的长度,因此我想使用Scrollable MessageBox.

我从2007年开始发现这篇文章:Mike Gold 2007年7月30日

http://www.c-sharpcorner.com/UploadFile/mgold/ScrollableMessageBox07292007223713PM/ScrollableMessageBox.aspx

现在,2011年还有其他好的组件吗?我想评估一些有关它的组件.

更新:

另一个组件,但更旧:MessageBoxExLib http://www.codeproject.com/KB/dialog/MessageBoxEx.aspx

可自定义的.NET Winforms消息框. http://www.codeproject.com/KB/dialog/Custom_MessageBox.aspx

jre*_*ert 9

拿这个:FlexibleMessageBox - .NET MessageBox的灵活替代品

它是一个经过测试的类,可以无缝地替换您的所有用法,MessageBox.Show并允许您在单个类文件中获得更多功能,您可以轻松地将其添加到项目中.

  • FlexibleMessageBox 类很棒。我已将文件放入 pastebin 以便于访问 - https://pastebin.com/UWJuP7VY (2认同)

Kei*_*thS 4

当我需要类似的东西来显示长状态报告或应用程序捕获的异常时,我刚刚实现了一个带有可滚动多行文本框的简单表单。如果您愿意,您可以更改边框等,使其看起来更像标签。然后只需新建一个并调用其 ShowDialog 方法,或将其实例化包装在类似于 MessageBox 的某个静态成员中。据我所知,.NET 团队还没有内置任何比 MessageBox 更灵活的东西。

从评论中编辑:

创建带有可以使用静态方法显示的文本框的窗口的代码相当简单。虽然我通常不喜欢公开请求代码,但这次我会答应:

public class SimpleReportViewer : Form
{
    /// <summary>
    /// Initializes a new instance of the <see cref="SimpleReportViewer"/> class.
    /// </summary>
    //You can remove this constructor if you don't want to use the IDE forms designer to tweak its layout.
    public SimpleReportViewer()
    {
        InitializeComponent();
        if(!DesignMode) throw new InvalidOperationException("Default constructor is for designer use only. Use static methods instead.");
    }

    private SimpleReportViewer(string reportText)
    {
        InitializeComponent();
        txtReportContents.Text = reportText;
    }

    private SimpleReportViewer(string reportText, string reportTitle)
    {
        InitializeComponent();
        txtReportContents.Text = reportText;
        Text = "Report Viewer: {0}".FormatWith(reportTitle);
    }

    /// <summary>
    /// Shows a SimpleReportViewer with the specified text and title.
    /// </summary>
    /// <param name="reportText">The report text.</param>
    /// <param name="reportTitle">The report title.</param>
    public static void Show(string reportText, string reportTitle)
    {
        new SimpleReportViewer(reportText, reportTitle).Show();
    }

    /// <summary>
    /// Shows a SimpleReportViewer with the specified text, title, and parent form.
    /// </summary>
    /// <param name="reportText">The report text.</param>
    /// <param name="reportTitle">The report title.</param>
    /// <param name="owner">The owner.</param>
    public static void Show(string reportText, string reportTitle, Form owner)
    {
        new SimpleReportViewer(reportText, reportTitle).Show(owner);
    }

    /// <summary>
    /// Shows a SimpleReportViewer with the specified text, title, and parent form as a modal dialog that prevents focus transfer.
    /// </summary>
    /// <param name="reportText">The report text.</param>
    /// <param name="reportTitle">The report title.</param>
    /// <param name="owner">The owner.</param>
    public static void ShowDialog(string reportText, string reportTitle, Form owner)
    {
        new SimpleReportViewer(reportText, reportTitle).ShowDialog(owner);
    }

    /// <summary>
    /// Shows a SimpleReportViewer with the specified text and the default window title.
    /// </summary>
    /// <param name="reportText">The report text.</param>
    public static void Show(string reportText)
    {
        new SimpleReportViewer(reportText).Show();
    }

    /// <summary>
    /// Shows a SimpleReportViewer with the specified text, the default window title, and the specified parent form.
    /// </summary>
    /// <param name="reportText">The report text.</param>
    /// <param name="owner">The owner.</param>
    public static void Show(string reportText, Form owner)
    {
        new SimpleReportViewer(reportText).Show(owner);
    }

    /// <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()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SimpleReportViewer));
        this.txtReportContents = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // txtReportContents
        // 
        this.txtReportContents.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                                                                               | System.Windows.Forms.AnchorStyles.Left)
                                                                              | System.Windows.Forms.AnchorStyles.Right)));
        this.txtReportContents.Location = new System.Drawing.Point(13, 13);
        this.txtReportContents.Multiline = true;
        this.txtReportContents.Name = "txtReportContents";
        this.txtReportContents.ReadOnly = true;
        this.txtReportContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
        this.txtReportContents.Size = new System.Drawing.Size(383, 227);
        this.txtReportContents.TabIndex = 0;
        // 
        // SimpleReportViewer
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(408, 252);
        this.Controls.Add(this.txtReportContents);
        this.Icon = Properties.Resources.some_icon;
        this.Name = "SimpleReportViewer";
        this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
        this.Text = "Report Viewer";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private TextBox txtReportContents;
}
Run Code Online (Sandbox Code Playgroud)

用法:

var message = GetSomeRidiculouslyLongMessage();
//assumes it's called from inside another Form
SimpleReportViewer.ShowDialog(message, "My Message", this);
Run Code Online (Sandbox Code Playgroud)

此特定实现还支持使用 Show() 方法的重载显示为普通的非对话框窗口。