C# - A Custom Windows Message Box

It's taken a while, but I've finally, updated a custom MessageBox class, that I had developed a few years ago. I wrote an article about it and posted it on CodeProject.com. You can read the original article here. In this article, I'll explain some of the new changes and why I made them.
In the new updated version, the message box form, will expand to accommodate long text. The previous version had a fixed sized window form, which meant large amounts of text would not be shown. Figure 1.1 Below shows a screen print of the old version.

Figire1.1


It's not obvious in the image but the message has been cut off. In the new version, I wanted to make sure that the message box expanded to fit the message text. Figure 1.2 below shows an image of the new version expanded to accommodate large messages.

Figure 1.2


As you can see then design has changed and is more in-line with a metro look. The new version also makes it possible to select from a predefined set of icons. These icons are just a wrapper for the icons found in the SystemIcons class. The return value from clicking on a MessagBox button is no longer an integer. Instead MessageBox buttons now return a DialogResult object. The code below illustrates the new way of checking what button was clicked.
  1. DialogResult result = MsgBox.Show("Are you sure you want to exit?", "Exit", MsgBox.Buttons.YesNo, MsgBox.Icon.Info, MsgBox.AnimateStyle.ZoomIn);
  2.  
  3. if (result == DialogResult.Yes)
  4. {
  5.  MessageBox.Show("Exiting now");
  6. }

As you can see above the code to check, which button was clicked is no different to how you would check, which button was clicked when using .Net MessageBox class. This makes the code more portable.

And finally, Ive also added three animation styles, that can be used to animate the opening of the form. The three animation styles are SlideDown, FadeIn and ZomeIn. You can download the new updated version from CodeProject.com or clicking the download button below. If you find this project useful, please leave your comments and if possible click the like button.

Download For C# Download For VB

You might also be interested in this custom inputbox for c#.
Download For C#

25 comments:

Unknown said...

Respected Sir,
Your project is just awesome for me. Specially latest version. i'm using your custom message box to my project and its look great, but when i'm implement in my project, i notice a little problem so i think you must resolve this issue. the problem is that if any one use first show() that only display message, then the project is stuck after message box shown because once's message box appear, then there is no way to close this message box. so i think you can put a close button or default OK button or esc button click event so user can go after this message box shown.....

Unknown said...

Hi Vivek, thank you for your comment. I will certainly look into the problem you mentioned and update the project.

Unknown said...

Hi Syed
it's impossible to download the code from the link of the button. Could you check please ?

Unknown said...

My apologies to all who tried to download with problems, Ive since moved the files to a different hosting account.

Anonymous said...

This is truely amazing. A friend gave me your old version code and i never knew where it was from. I always wondered how i could get it to fit to the text. Now I do. Thanks. Happy coding! :) - magnet_man16

Raza Rizvi said...

Thanks a bunch! Works like a charm.

Unknown said...

Very well done! I was gonna create something similar with the same design for my application but you saved me a lot of time. Great work!

HeartWare said...

Excellent work. Used it as a base for a more versatile implementation, where you can select the exact buttons you want, as is:

New MsgBox().AddYesButton().AddNoButton().AddAbortButton().Show(<Message>,<Title>[,<Icon>[,<Animate>]])

Or even

New MsgBox().AddButton("Custom Text",DialogResult.Ignore).AddNoButton("Nein (No in German)").Show(<Message>,<Title>[,<Icon>[,<Animate>]])

The updated MsgBox.cs can be downloaded here:

http://textuploader.com/kd8u

HeartWare said...

Fixed a few errors, and changed interface. MsgBox.Show methods are now static functions, and .ShowDialog methods operate on an instance of the class.

New version

Unknown said...

Hi HeartWare, I'm glad the project helped. Good work on the customizable buttons.

Unknown said...

I added this but when I call it like your example I see a window in the taskbar but nothing is showing. Its hidden and cant get ride of it?

Unknown said...

Hi Mike, thank you for your comment. Remove the animation parameter and let me know if the form is still not visible.

Unknown said...

Great Job man! Guess I wont be using the native MessageBox anymore!

McLoco said...

Hi! Very nice control. The only drawback I've found is NewLines. If you use Enviroment.NewLine the text structure breaks and the window is wrong resized... :(

Johel Alvarez said...

Excellent work.
Thanks a lot.

Pedram said...

Oops I don't know if Google forgot to post my comment Syed Hussain but could you please help me with this issue regarding your message box. Everything else work perfectly and I thank you for you implementation. http://stackoverflow.com/questions/26933304/form-hides-behind-the-other-form-after-showdialog

Unknown said...

Hi Petram, has this been resolved in statckoverflow?

Dyhrberg said...

Hi Syed

Nice job on the dialog. Noticed a small issue that I suggest that you improve.
Have an application in full screen, and when I close the dialog, it causes my full screen program to flicker shortly.
Believe the problem is the abrupt kill of the dialog you do when your done with it.
Suggest that you replace the xxxx.Dispose() with the more appropriate xxxx.Close()
This resolved my problem.

I found the solution here:
http://stackoverflow.com/questions/26933304/form-hides-behind-the-other-form-after-showdialog

Dyhrberg said...

Hi Syed

For my purpose I need the Dialog to open up without any buttons, and instead close when a timer expired.
I have implemented that clean into your existing structure.
If you would like the source for this function, to include into your base code, let me know.

//Dyhrberg

Noah liam said...

Your article has really good reading, thought-provoking information. You make several sound points I find agreeable. Your article is a good example of superior writing from a writer that cares. Thanks for sharing this information. Custom Window Boxes

Mohamed gida said...

Good work thank you
I want to ask you if i can to make the title ,icon , messagelabel and buttons shown in the center of form
I try meny times
Thank you

Chris Michael said...
This comment has been removed by the author.
Lucas said...

Super lækkert mad med virkelig god smag, der var kikset noget med indisk take away bestillingen, men det klarede de også, tak for dejlig mad

Lucas said...

Vi besøgte Bindia en lørdag aften og fik dejlig mad.Indisk Restaurant København Det var lige så godt, som vi huskede det. Denne gang valgte jeg Lamb Shorba,Indian restaurant og det er en ret med meget smag. Den kan anbefales, hvis man er til stærk mad. Vi startede med samosa...de er bare helt fantastiske!

Chris Michael said...
This comment has been removed by the author.

Post a Comment