Click here to Skip to main content
Click here to Skip to main content
Go to top

Advanced message box for .NET

, 6 Oct 2008
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Sergey Stoyan
Architect CliverSoft (www.cliversoft.com)
Ukraine Ukraine
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

Comments and Discussions

 
GeneralFunny... [modified] Pinmember93Current27-Jul-09 7:33 
GeneralNot working Pinmemberstann43511-Dec-08 0:09 
GeneralRe: Not working PinmemberSergey Stoyan13-Dec-08 5:22 
GeneralRe: Not working Pinmemberstan_p15-Dec-08 22:15 
GeneralRe: Not working PinmemberSergey Stoyan15-Dec-08 22:53 
GeneralRe: Not working Pinmemberstan_p10-Jan-09 18:22 
GeneralNice Pinmemberashu fouzdar19-Aug-08 3:21 
GeneralMessage blocks UI thread if called when another thread is already showing a message Pinmembersupercat925-Jul-08 14:08 
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan25-Jul-08 23:07 
GeneralRe: Message blocks UI thread if called when another thread is already showing a message Pinmembersupercat926-Jul-08 8:48 
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan29-Jul-08 2:22 
GeneralRe: Message blocks UI thread if called when another thread is already showing a message [modified] PinmemberSergey Stoyan13-Aug-08 0:46 
GeneralVery good... a request for future upgrades Pinmemberangel.escamilla1-Apr-08 12:39 
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan1-Apr-08 20:38 
GeneralRe: Very good... a request for future upgrades PinmemberThe_Mega_ZZTer25-Jul-08 5:10 
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan25-Jul-08 22:46 
GeneralRe: Very good... a request for future upgrades PinmemberSergey Stoyan27-Jul-08 23:37 
GeneralRe: Very good... a request for future upgrades Pinmemberangel.escamilla20-Feb-09 22:11 
GeneralColors PinmemberGeno Carman25-Mar-08 13:36 
GeneralRe: Colors PinmemberSergey Stoyan25-Mar-08 21:23 
GeneralVery cool PinmemberMaxGuernsey24-Mar-08 15:58 
GeneralRe: Very cool [modified] PinmemberSergey Stoyan24-Mar-08 22:35 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 6 Oct 2008
Article Copyright 2008 by Sergey Stoyan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid