A Scrollable MessageBox in C#

27,145

Solution 1

Take this one: FlexibleMessageBox – A flexible replacement for the .NET MessageBox

It is a tested class which seamlessly replaces all your usages of MessageBox.Show and lets you get more features in a single class file you can easily add to your project.

Solution 2

I just implemented a simple form with a scrollable multiline TextBox, when I needed something similar to show long status reports or exceptions caught by the application. You can alter borders, etc. to make it look more like a label, if you wish. Then just new one up and call its ShowDialog method, or wrap its instantiation in some static member similar to MessageBox. Far as I know, the .NET team hasn't built in anything more flexible than MessageBox.

EDIT FROM COMMENT:

The code to make a window with a textbox that can be shown using a static method is fairly simple. While I generally don't like overt requests for code, I'll oblige this time:

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

Usage:

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

This particular implementation also supports displaying as an ordinary, non-dialog window using overloads of the Show() method.

Solution 3

I was looking for a scrollable messagebox for WPF - and then I found MaterialMessageBox, which is an (in my opinion) nice looking alternative to FlexibleMessageBox for WPF. You can download it from GitHub here: https://github.com/denpalrius/Material-Message-Box or install it as a nuget package inside Visual Studio (https://www.nuget.org/packages/MaterialMessageBox/).

The only problem I found was that you can't use it from outside the main thread, so my solution to that was to invoke the main thread and show the message from there: mainWindow.Dispatcher.Invoke(() => MaterialMessageBox.Show("message text"));

Share:
27,145
Kiquenet
Author by

Kiquenet

Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts What have you tried? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments Asking http://stackoverflow.com/help/asking Answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers http://www.enriquepradosvaliente.com http://kiquenet.wordpress.com ◣◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤ ◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤◢ .NET developer and fan of continuous self-improvement and good patterns and practices. Stuff I am interested in: .NET technology stack in general, C#, Powershell and Javascript in particular as languages Test driven development, DI, IoC and mocking frameworks Data access with ORMs and SQL ASP.NET javascript, jQuery and related frontend frameworks Open source projects

Updated on August 02, 2020

Comments

  • Kiquenet
    Kiquenet over 3 years

    I use Addin in VS2008, C#, and I need show messages (error messages and others).

    I don't know the length of messages, and therefore I want use Scrollable MessageBox.

    I have found this article from 2007 year: By Mike Gold July 30, 2007

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

    now, in 2011 any another good components ?? I want evaluate several components about it.

    Update:

    another component but older: MessageBoxExLib http://www.codeproject.com/KB/dialog/MessageBoxEx.aspx

    A customizable .NET Winforms Message Box. (NOTE: this is now deleted) http://www.codeproject.com/KB/dialog/Custom_MessageBox.aspx

  • Taran
    Taran over 7 years
    Westec seems to be a company name referring to internal components. To make this work, you need to replace 'new Westec.CommonLib.Presentation.Controls.WestecTextBox();' with 'new TextBox()', 'WestecTextBox' with 'TextBox', 'Westec.CommonLib.Presentation.Properties.Resources.interfac‌​e_icon;' with 'SystemIcons.Error' (or other icon of your choice).
  • Taran
    Taran over 7 years
    Disclaimer: The author of this answer wrote the linked code. However, having tried it, it is the best solution and much better looking with a better interface than the solution below. Would be nice if you hosted it somewhere like github which didn't require registration...
  • KeithS
    KeithS over 7 years
    Yeah, sorry about that. Westec was my old company, now defunct, and the "WestecTextBox" was a custom extension of a vanilla textbox incorporating some additional features like cue banner support and spell-check plugins. There was nothing in the custom control that is needed for this code to work. I have sanitized the sample for posterity.
  • csharpforevermore
    csharpforevermore about 7 years
    Would be even better if it was hosted in NuGet as it's frustrating having to copy + paste the code from the Codeplex project.
  • Hudson
    Hudson over 6 years
    Thanks! Much appreciated!
  • Jack
    Jack about 5 years
    The FlexibleMessageBox Class is great. I've put the file into pastebin for easy acces - pastebin.com/UWJuP7VY
  • jreichert
    jreichert about 5 years
    @Jack: Thx! :-)