2011-01-13 45 views
3

私はVS2008、C#でAddinを使用しています。表示メッセージ(エラーメッセージなど)が必要です。C#のスクロール可能なMessageBox

私はメッセージの長さを知らないので、私はScrollable MessageBoxを使いたいです。

私は2007年から、この記事を見つけた:マイク・ゴールド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

+2

マイカスタマイズ.NET Winformsメッセージボックスはスクロールできません。テキストは自動的に伸びてテキストに合わせます。大規模なスタックトレースでエラーメッセージを表示すると、ボックスが大きすぎる可能性があります。これを回避するには、エラーメッセージを表示し、メッセージボックスにカスタムボタン "show stack trace"を追加したり、 "スタックトレースをクリップボードにコピー"を追加したりすることができます。 – MaxK

答えて

2

私は、スクロール可能な複数行のTextBoxを使用して簡単なフォームを実装しました。これは、アプリケーションによって取得された長いステータスレポートまたは例外を表示するのに似たものが必要なときに適用されました。あなたが望むならば、ラベルのように見えるように罫線などを変更することができます。それから、新しいインスタンスを作成してShowDialogメソッドを呼び出すか、MessageBoxと同様の静的メンバでそのインスタンス化をラップします。私の知る限り、.NETチームはMessageBoxよりも柔軟性のあるものを組み込んでいません。 COMMENT FROM

EDIT:かなり単純で静的メソッドを使用して示すことができるテキストボックスでウィンドウを作るため

コード。私は一般的に、コードのための明白な要求が好きではありませんが、私は、この時間を義務付けるます:

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()メソッドのオーバーロード。

+1

codeprojectの代替案のように投稿しましたか? – Kiquenet

+0

"westec"とは何ですか? – rolls

+0

Westecは内部コンポーネントを参照する会社名のようです。 これを行うには、 'new Westec.CommonLib.Presentation.Controls.WestecTextBox();'を置き換える必要があります。 'New TextBox()'、 'WestecTextBox' with 'TextBox'、 'Westec.CommonLib.Presentation.Properties.Resources.interface_icon;' 'SystemIcons.Error'(またはあなたが選択した他のアイコン)を使用します。 – Taran

6

は、このいずれかを取る:FlexibleMessageBox – A flexible replacement for the .NET MessageBox

それがシームレスMessageBox.Showのすべてのあなたの用法を置き換えて、あなたが簡単にあなたのプロジェクトに追加することができ、単一のクラスファイル内でより多くの機能を得ることができますテストしたクラスです。

+1

免責事項:この回答者はリンクされたコードを書いています。しかし、これを試してみると、それは最良の解決策であり、以下の解決策よりも優れたインターフェースで見栄えがよくなります。あなたが登録を必要としなかったgithubのようなどこかでそれをホストしていればいいでしょう... – Taran

+0

NuGetでホストされていた方が良いでしょう。Codeplexプロジェクトのコードをコピーして貼り付けなくてはいけません。 – csharpforevermore

+0

ありがとう!とても有難い! – Hudson

0

私はWPFのためのスクロール可能なメッセージボックスを探していました。そして、私の考えでは、WPFのためのFlexibleMessageBoxの代わりにいい見た目のMaterialMessageBoxが見つかりました。 GitHubからダウンロードすることができます:https://github.com/denpalrius/Material-Message-Box、またはVisual Studio(https://www.nuget.org/packages/MaterialMessageBox/)内にナゲットパッケージとしてインストールしてください。

私が見つけた唯一の問題はそれに私の解決策は、メインスレッドを起動し、そこからメッセージを表示するようにしたので、あなたは、メインスレッド外からそれを使用することはできませんということでした。 mainWindow.Dispatcher.Invoke(() => MaterialMessageBox.Show("message text"));

関連する問題