.Net super MessageBox
July 29, 2008 .Net, Php — Tags: .net messagebox, MessageBox, winform, winform messagebox — 52coding
Some applications need a more advanced message box than MessageBox class provided by .NET. This article presents a .NET library called MessageForm that can be used instead of .NET MessageBox getting more features and improvements. The MessageForm library consists of 2 classes:
MessageForm, that is, in fact, the multi-button message box implementation;Message, a wrapper forMessageForm
Message Class
The goal of Message is to make use of the multi-button message box in your code as simple and fast as possible. It provides a collection of predefined MessageForm instances most frequently used. That means you have usually to work with Message class only in your code. If its predefined methods do not meet your needs, you can use MessageForm directly or enhance Message with your own ones.
Message is a thread-safe class so if you have several threads that use it simultaneously, Message will show message boxes in turn with no collision. If you want to show message boxes simultaneously, use MessageForm class.
聽
Default Settings in Message Class
- the message window’s icon is the icon of the hosting application;
- the capture text is the application’s name;
- buttons are colored with different colors. Often it is handy as helps the user faster select the desired answer. Only OK-button message box has uncolored button;
- message window is a top-most and top-level as it is usually needed for alert messages;
These settings are the respective attributes of Message and so can be changed from your code that uses MessageForm or even directly within MessageForm project. For example coloration of the buttons can be toggled off by the following code:
<span class="code-comment">//</span><span class="code-comment">toggle off coloring buttons</span> CliverSoft.Message.ButtonColors = <span class="code-keyword">null</span>;
Usage of MessageForm
Below are several examples of using Message and MessageForm classes. (More examples you can find in Test project within MessageForm solution.) The respective code follows image.
Using Message Class
Message box with OK button:
CliverSoft.Message.Inform(<span class="code-string">"</span><span class="code-string">Message.Ok test"</span>);
Message box with 2 custom buttons:
<span class="code-comment">//</span><span class="code-comment">returns clicked button index</span>
<span class="code-keyword">int</span> a = CliverSoft.Message.Show(SystemIcons.Error, <span class="code-string">"</span><span class="code-string">Connection lost"</span>, <span class="code-digit">0</span>,
<span class="code-string">"</span><span class="code-string">Try to reconnect"</span>, <span class="code-string">"</span><span class="code-string">Exit"</span>);Message box with 3 custom buttons and checkbox
<span class="code-comment">//</span><span class="code-comment">Set message box caption once and forever </span>
CliverSoft.Message.Caption = <span class="code-string">"</span><span class="code-string">Backup Application"</span>;
<span class="code-comment">//</span><span class="code-comment">returned silent box state </span>
<span class="code-keyword">bool</span> r;
<span class="code-comment">//</span><span class="code-comment">returned clicked button index</span>
<span class="code-keyword">int</span> a;
<span class="code-comment">//</span><span class="code-comment">show message box with 3 buttons</span>
a = CliverSoft.Message.Show(SystemIcons.Information, <span class="code-keyword">out</span> r,
<span class="code-string">@"</span><span class="code-string">Local file c:\test.txt differs from the backup copy"</span>, <span class="code-digit">0</span>,
<span class="code-string">"</span><span class="code-string">Backup the newest file"</span>,
<span class="code-string">"</span><span class="code-string">Download the backup copy"</span>,
<span class="code-string">"</span><span class="code-string">Do nothing for now"</span>
);What if message box has to display unpredictably long text, for instance an error stack? Sometimes the message can be so long that .NET MessageBox will go out of the screen making the end of the message invisible. Here is an example how MessageForm treats such ‘unsafe’ cases:
Using MessageForm Directly
Message box with 5 buttons, checkbox and jpg as message icon (an example of direct MessageForm use):
]
CliverSoft.MessageForm mf = new MessageForm(
“Using MessageForm directly”,//caption
“Hi, I’m genie from a bottle. Please choose the option you prefer:”,//message
new Color[5] {
Color.LightBlue,
Color.Lavender,
Color.LightPink,
Color.LemonChiffon,
Color.Empty
},//button colors
new string[5] {
“have nice holidays on seaside resort”,
“have an interesting project”,
“have a happy love”,
“be a favorite of my boss”,
“I want it all together”
},//array of answers
4,//default button
“Do not forget to ask about it again in the week.”,//silent box text
new Bitmap(“../../image.jpg”)//message icon, it can be Image type
);
//set icon in the capture
Bitmap b = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new System.Drawing.SolidBrush(Color.RosyBrown),
new Rectangle(0, 0, 16, 16));
mf.Icon = Icon.FromHandle(b.GetHicon());
//show message form
mf.GetAnswer();
//getting state of silent checkbox
bool silent = mf.Silent;聽
MessageForm library is written in pure C#. It builds its own window that’s not a wrapper of MessageBox class. It inherits System.Windows.Forms.Form so you can easily and flexibly modify it as you want by changing its code.
Also be aware that while automatic arranging controls in MessageForm the following values are not changed from their initial values:
minimal size of the window (it can only grow);
the message icon鈥檚 location;
the message label鈥檚 Y value;
span between window鈥檚 left edge and the first button;
Keeping that in your mind, you can tune the look of MessageForm in Visual Studio window design mode.
Also pay attention to several attributes in MessageFrom class that define its layout as well.
Using the Code
In the attached code you can find:
library project MessageForm that contains the MessageForm and Message classes. These classes can be compiled as a dll or be added to your code.
Test project with the usage examples.
