C#中的Scrollable 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
拿这个: FlexibleMessageBox – .NET MessageBox的灵活替代品
它是一个经过测试的类,可以无缝地替换MessageBox.Show
所有用法,并允许您在单个类文件中获得更多function,您可以轻松地将其添加到项目中。
我刚刚实现了一个带有可滚动多行TextBox的简单表单,当我需要类似于显示长状态报告或应用程序捕获的exception时。 如果您愿意,您可以更改边框等,使其看起来更像标签。 然后只需要新的一个并调用它的ShowDialog方法,或将其实例化包装在类似于MessageBox的静态成员中。 据我所知,.NET团队没有内置比MessageBox更灵活的东西。
编辑评论:
使用可以使用静态方法显示的文本框创建窗口的代码非常简单。 虽然我一般不喜欢公开的代码请求,但这次我要求:
public class SimpleReportViewer : Form { /// /// Initializes a new instance of the class. /// //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); } /// /// Shows a SimpleReportViewer with the specified text and title. /// /// The report text. /// The report title. public static void Show(string reportText, string reportTitle) { new SimpleReportViewer(reportText, reportTitle).Show(); } /// /// Shows a SimpleReportViewer with the specified text, title, and parent form. /// /// The report text. /// The report title. /// The owner. public static void Show(string reportText, string reportTitle, Form owner) { new SimpleReportViewer(reportText, reportTitle).Show(owner); } /// /// Shows a SimpleReportViewer with the specified text, title, and parent form as a modal dialog that prevents focus transfer. /// /// The report text. /// The report title. /// The owner. public static void ShowDialog(string reportText, string reportTitle, Form owner) { new SimpleReportViewer(reportText, reportTitle).ShowDialog(owner); } /// /// Shows a SimpleReportViewer with the specified text and the default window title. /// /// The report text. public static void Show(string reportText) { new SimpleReportViewer(reportText).Show(); } /// /// Shows a SimpleReportViewer with the specified text, the default window title, and the specified parent form. /// /// The report text. /// The owner. public static void Show(string reportText, Form owner) { new SimpleReportViewer(reportText).Show(owner); } /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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()方法的重载显示为普通的非对话窗口。
我正在为WPF寻找一个可滚动的消息框 – 然后我找到了MaterialMessageBox,这是(在我看来)对于WPF的FlexibleMessageBox来说是一个很好看的替代品。 您可以从GitHub下载它: https : //github.com/denpalrius/Material-Message-Box或在Visual Studio中将其安装为nuget包( https://www.nuget.org/packages/MaterialMessageBox/ )。
我发现的唯一问题是你不能在主线程之外使用它,所以我的解决方案是调用主线程并从那里显示消息: mainWindow.Dispatcher.Invoke(() => MaterialMessageBox.Show("message text"));