C#中的Scrollable MessageBox

时间:2011-01-13 15:44:30

标签: 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

3 个答案:

答案 0 :(得分:8)

拿一个:FlexibleMessageBox – A flexible replacement for the .NET MessageBox

这是一个经过测试的课程,可以无缝替换MessageBox.Show的所有用法,让您可以在一个类文件中获得更多功能,您可以轻松添加到项目中。

答案 1 :(得分:2)

我刚刚实现了一个带有可滚动多行TextBox的简单表单,当我需要类似于显示长状态报告或应用程序捕获的异常时。如果您愿意,您可以更改边框等,使其看起来更像标签。然后只需要新的一个并调用它的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;
}

用法:

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

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

答案 2 :(得分:0)

我正在为WPF寻找一个可滚动的消息框 - 然后我找到了MaterialMessageBox,这是(我认为)用于WPF的FlexibleMessageBox的漂亮外观。您可以从GitHub下载它:https://github.com/denpalrius/Material-Message-Box或将其作为nuget包安装在Visual Studio(https://www.nuget.org/packages/MaterialMessageBox/)中。

我发现的唯一问题是你不能在主线程之外使用它,所以我的解决方案是调用主线程并从那里显示消息: mainWindow.Dispatcher.Invoke(() => MaterialMessageBox.Show("message text"));