Click here to Skip to main content
Licence CPOL
First Posted 17 Mar 2008
Views 51,645
Downloads 228
Bookmarked 97 times

Advanced message box for .NET

By Sergey Stoyan | 6 Oct 2008
A replacement of .NET MessageBox class that provides additional features and improvements like possibility to show any number of buttons, ‘apply-my-answer-by-default’ checkbox, intelligence layout and more.

1
4 votes, 23.5%
2
5 votes, 29.4%
3
3 votes, 17.6%
4
5 votes, 29.4%
5
3.51/5 - 17 votes
μ 3.51, σa 2.06 [?]

Introduction

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.

General Features of MessageForm Comparing with MessageBox

Features

MessageForm

MessageBox

can contain any number of buttons

+

-
(provides only up 3 predefined buttons)

text and color of the buttons are specified by parameters

+

-

can display ‘apply-my-answer-by-default’ checkbox so that user can make his/her answer applied by default in the future

+

-

allows selecting and copying message; it is handy thing when you want to get path or error string from message box + -

window's content can be seen independently on its size

+
(MessageForm’s size is always less than the display; if its content is too large then the scroll bar is provided)

-
(if message is too long, it goes out of the screen so that you cannot read it all)

allows setting icon in the window's title bar

+
(by default it displays icon of the hosting application)

-

thread safe

+
(As an option, Message class also provides thread synchronization so that messages of different threads are showed strongly in turn)

+

brings message box to the top of windows on the desktop; it is important feature for alerts + +/-
(if the application showing message box is windowless, the message box can be from the beginnig "lost" under the rest windows on the desktop)

accepts Image type as well as Icon type when setting icon beside message

+

-

can be fitted/enhanced further

+
(open source)

-

Description

The MessageForm library consists of 2 classes:

  • MessageForm, that is, in fact, the advanced message box implementation;
  • Message, a wrapper for MessageForm

MessageForm Class

MessageForm class implements the message box window basing on System.Windows.Forms.Form. Usually you do not want to use it because of using Message class instead. On the other hand, MessageForm provides more flexible settings than Message does.

Message Class

The goal of Message is to make use of the advanced message box in your code as simple as possible. It provides a collection of predefined MessageForm instances most frequently used. That means you have usually to work with Message class only. 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 that you can use it from within several threads with no collision. The Message.ShowMessagesInTurn parameter defines how message boxes will be showed in a multithread environment. If it is true then message boxes are showed one after another. If it is false, Message shows message boxes invoked from different threads, simultaneously (that's like .NET MessageBox does).

The Message class has the following default settings:

  • the icon dispalyed in the title bar of a message window is an icon of the hosting application;
  • the text in the title bar is the hosting application's name;
  • buttons are colored with different colors. Often it is handy as it helps a user faster select a desired answer. Only the OK-button message box has uncolored button;
  • message window is a top-most and top-level as it is usually needed for alert messages;
  • message boxes invoked from different threads are showed in turn one after another;

These settings are the respective attributes of Message and so can be changed from your code that uses MessageForm or even directly within the MessageForm project. For example, coloration of the buttons can be toggled off by the following code:

//toggle off coloring buttons
Cliver.Message.ButtonColors = null;

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 the image.

Using Message Class

Message box with OK button (XP and Vista style):

1-button.JPG 1-button.JPG

Cliver.Message.Inform("Message.Ok test");

Message box with 2 custom buttons (XP and Vista style):

2-buttons.JPG 2-buttons.JPG

//returns clicked button index
int a = Cliver.Message.Show(SystemIcons.Error, "Connection lost", 0,
    "Try to reconnect", "Exit"); 

Message box with 3 custom buttons and checkbox:

3-buttons.JPG

 //Set message box caption once and forever 
 Cliver.Message.Caption = "Backup Application";

 //returned silent box state 
 bool r;
 //returned clicked button index
 int a;

 //show message box with 3 buttons
 a = Cliver.Message.Show(SystemIcons.Information, out r,
                @"Local file c:\test.txt differs from the backup copy", 0,
                "Backup the newest file",
                "Download the backup copy",
                "Do nothing for now"
                );

What if message box has to display unpredictably long text, for instance, an error stack? Sometimes the message can be so long that a .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:

scrollbar3.JPG

Using MessageForm Directly

Message box with 6 buttons, checkbox and custom message icon. Notice that MessageForm can accept Image type in place of Icon type.

MessageForm.JPG

How it looks in the classic style on screen with less resolution:

MessageForm2.JPG
Cliver.MessageForm mf = new MessageForm(
    "Using MessageForm directly",//caption
    //message
    "Copying files from: 'c:\\test' to: 'c:\\test2'\nFile 'test.txt' already exists.",
    new Color[6] {
        Color.LightCoral,
        Color.LightYellow,
        Color.Empty,
        Color.LightGreen,
        Color.LightBlue,
        Color.Empty,
    },//button colors
    new string[6] { 
        "Overwrite",
        "Overwrite if older", 
        "Skip", 
        "Rename",
        "Append",
        "Cancel"
    },//array of answers
    1,//default button
    "Apply to all",//silent box text
    new Bitmap("../../copying.jpg")//message icon, can be set from image
);

//set icon in the capture
mf.Icon = new Icon("../../computers308.ico");

//show message form
mf.GetAnswer();

//get state of silent checkbox
bool silent = mf.Silent;

Modifying MessageForm

The MessageForm library is written in pure C#. It builds its own window that's not a wrapper of the 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’s location;
  • the message label’s Y value;
  • span between window’s 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 the MessageFrom class that defines 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.

The last version of MessageForm can be found at SourceForge

Be happy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sergey Stoyan

Architect
CliverSoft (www.cliversoft.com)
Ukraine Ukraine

Member
Sergey is graduated as applied mathematician. He is specialized in client/server applications, backup systems, data parsing tools, web crawlers and search engines. Work for CliverSoft Co. Favorite languages are C#, C++, Perl

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFunny... [modified] Pinmember93Current8:33 27 Jul '09  
GeneralNot working Pinmemberstann4351:09 11 Dec '08  
GeneralRe: Not working PinmemberSergey Stoyan6:22 13 Dec '08  
GeneralRe: Not working Pinmemberstan_p23:15 15 Dec '08  
GeneralRe: Not working PinmemberSergey Stoyan23:53 15 Dec '08  
GeneralRe: Not working Pinmemberstan_p19:22 10 Jan '09  
GeneralNice Pinmemberashu fouzdar4:21 19 Aug '08  
GeneralOK, but a nicer solution exists on CP PinmemberPSU Steve4:01 19 Aug '08  
GeneralRe: OK, but a nicer solution exists on CP Pinmembersk8er_boy28721:55 6 Oct '08  
GeneralRe: OK, but a nicer solution exists on CP PinmemberDonsw15:27 26 Jan '09  
GeneralMessage blocks UI thread if called when another thread is already showing a message Pinmembersupercat915:08 25 Jul '08  
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan0:07 26 Jul '08  
GeneralRe: Message blocks UI thread if called when another thread is already showing a message Pinmembersupercat99:48 26 Jul '08  
The issue is that if your message routine is called from a UI thread when another of your message boxes is showing, the locking primitive you use blocks all events on the UI thread. If you're going to try to control the behavior of message boxes that are shown on different threads, it would probably be good to do so in a fashion that works smoothly.
 
To demonstrate the issue I'm describing, create a form with a checkbox and two buttons. The checkbox is simply to verify that the form is "clickable". One of the buttons should pop up a message box by calling your message routine directly. The other should start a thread which then calls the message routine.
 
If the first button is clicked, the controls on the form will be disabled while the message box is displayed, but the form will still respond to Windows events (move, paint, etc.). If the second button is clicked, the message box will pop up but the controls on the form will still work "normally" even while the box is displayed (confirm that with the check box). If the first button is clicked while the message box is up, however, the form will stop responding to Windows events until the message box is acknowledged. If you drag the message box over the form, the contents will be erased and not redrawn. Worse, if another thread calls .Invoke on a UI object owned by the blocked thread, the thread too will be blocked.
 
I wish .net had included a nice way to run a message pump while waiting. Unfortunately, I don't know of any.
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan3:22 29 Jul '08  
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan1:46 13 Aug '08  
GeneralVery good... a request for future upgrades Pinmemberangel.escamilla13:39 1 Apr '08  
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan21:38 1 Apr '08  
GeneralRe: Very good... a request for future upgrades PinmemberThe_Mega_ZZTer6:10 25 Jul '08  
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan23:46 25 Jul '08  
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan0:37 28 Jul '08  
GeneralRe: Very good... a request for future upgrades Pinmemberangel.escamilla23:11 20 Feb '09  
GeneralColors PinmemberGeno Carman14:36 25 Mar '08  
GeneralRe: Colors PinmemberSergey Stoyan22:23 25 Mar '08  
GeneralVery cool PinmemberMaxGuernsey16:58 24 Mar '08  
GeneralRe: Very cool [modified] PinmemberSergey Stoyan23:35 24 Mar '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 6 Oct 2008
Article Copyright 2008 by Sergey Stoyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid