Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>EDIT FROM COMMENT:</p> <p>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:</p> <pre><code>public class SimpleReportViewer : Form { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="SimpleReportViewer"/&gt; class. /// &lt;/summary&gt; //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); } /// &lt;summary&gt; /// Shows a SimpleReportViewer with the specified text and title. /// &lt;/summary&gt; /// &lt;param name="reportText"&gt;The report text.&lt;/param&gt; /// &lt;param name="reportTitle"&gt;The report title.&lt;/param&gt; public static void Show(string reportText, string reportTitle) { new SimpleReportViewer(reportText, reportTitle).Show(); } /// &lt;summary&gt; /// Shows a SimpleReportViewer with the specified text, title, and parent form. /// &lt;/summary&gt; /// &lt;param name="reportText"&gt;The report text.&lt;/param&gt; /// &lt;param name="reportTitle"&gt;The report title.&lt;/param&gt; /// &lt;param name="owner"&gt;The owner.&lt;/param&gt; public static void Show(string reportText, string reportTitle, Form owner) { new SimpleReportViewer(reportText, reportTitle).Show(owner); } /// &lt;summary&gt; /// Shows a SimpleReportViewer with the specified text, title, and parent form as a modal dialog that prevents focus transfer. /// &lt;/summary&gt; /// &lt;param name="reportText"&gt;The report text.&lt;/param&gt; /// &lt;param name="reportTitle"&gt;The report title.&lt;/param&gt; /// &lt;param name="owner"&gt;The owner.&lt;/param&gt; public static void ShowDialog(string reportText, string reportTitle, Form owner) { new SimpleReportViewer(reportText, reportTitle).ShowDialog(owner); } /// &lt;summary&gt; /// Shows a SimpleReportViewer with the specified text and the default window title. /// &lt;/summary&gt; /// &lt;param name="reportText"&gt;The report text.&lt;/param&gt; public static void Show(string reportText) { new SimpleReportViewer(reportText).Show(); } /// &lt;summary&gt; /// Shows a SimpleReportViewer with the specified text, the default window title, and the specified parent form. /// &lt;/summary&gt; /// &lt;param name="reportText"&gt;The report text.&lt;/param&gt; /// &lt;param name="owner"&gt;The owner.&lt;/param&gt; public static void Show(string reportText, Form owner) { new SimpleReportViewer(reportText).Show(owner); } /// &lt;summary&gt; /// Required designer variable. /// &lt;/summary&gt; private System.ComponentModel.IContainer components = null; /// &lt;summary&gt; /// Clean up any resources being used. /// &lt;/summary&gt; /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt; protected override void Dispose(bool disposing) { if (disposing &amp;&amp; (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// &lt;/summary&gt; 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; } </code></pre> <p>Usage:</p> <pre><code>var message = GetSomeRidiculouslyLongMessage(); //assumes it's called from inside another Form SimpleReportViewer.ShowDialog(message, "My Message", this); </code></pre> <p>This particular implementation also supports displaying as an ordinary, non-dialog window using overloads of the Show() method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload