Friday, December 4, 2009

WPF MessageBox - Custom control


One feature I wanted in WPF applications is a good looking message box. But every time I call the MessageBox.Show() method in my apps, the old win forms style message box pops on my screen. Sad part is that the MessageBox class used in WPF goes directly into user32... which makes it impossible to extend the message box and apply some style to make it look more WPF way.
I decided to create a custom message box that looks better than the Win32 one and use in my WPF applications. Special thanks to Christian Moser for the sample to remove the title icon from a WPF window, which I have shamelessly copied in my implementation. The final output is as in the figure.
 
You can call the WPFMessageBox.Show method as:
__Result = WPFMessageBox.Show("WPF MessageBox", "This options displays a message box with YES, NO, CANCEL option!!!", WPFMessageBoxButtons.YesNoCancel, WPFMessageBoxImage.Question);

Implementation:
public static WPFMessageBoxResult Show(string title, string message, WPFMessageBoxButtons buttonOption, WPFMessageBoxImage image)
{
    ___MessageBox = new WPFMessageBox();
    MessageBoxViewModel __ViewModel = new MessageBoxViewModel(___MessageBox, title, message, buttonOption, image);
    ___MessageBox.DataContext = __ViewModel;
    ___MessageBox.ShowDialog();
    return ___MessageBox.Result;
}


 <Image Source="{Binding MessageImageSource}" HorizontalAlignment="Left" VerticalAlignment="Center" Height="48" Width="48" Margin="10 0 0 0" />
<TextBlock Text="{Binding Message}" Grid.RowSpan="2" Grid.Column="1" TextWrapping="Wrap" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10 10 10 10" />
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 5 0">           
     <Button Content="_Yes" Visibility="{Binding YesNoVisibility}" Command="{Binding YesCommand}" Margin="5 5 5 5" Height="24" Width="80" />
     <Button Content="_No" Visibility="{Binding YesNoVisibility}" Command="{Binding NoCommand}" Margin="5 5 5 5" Height="24" Width="80" />
     <Button Content="_Cancel" Visibility="{Binding CancelVisibility}" Command="{Binding CancelCommand}" Margin="5 5 5 5" Height="24" Width="80" />
     <Button Content="O_K" Visibility="{Binding OkVisibility}" Command="{Binding OkCommand}" Margin="5 5 5 5" Height="24" Width="80" />
     <Button Content="_Close" Visibility="{Binding CloseVisibility}" Command="{Binding CloseCommand}" Margin="5 5 5 5" Height="24" Width="80" />
StackPanel>

I have uploaded the source code to codeplex. You can get it from here
http://prajeeshprathap.codeplex.com/SourceControl/PatchList.aspx



73 comments:

Anonymous said...

Looks good.
Can I get the source code.
Have you tried creating a control which has an option to expand an show the inner exception.

Prajeesh said...

Currently the message box control does not have the feature to show the inner exception with an expander. I'll try to add this functionality to the control and post it soon. Thanks for bringing this to my attention.

Anonymous said...

Looks good, please could I have the src?

chrisblanchard@gmx.co.uk

thanks

Henk said...

This is what I am looking for.
Thanks!

Henk said...

This is what I am looking for. Thanks!

Henk said...

Great!

Anonymous said...

Great!

Henk said...

Looks great. Can I have the source code?

Anonymous said...

Very good, please send source to
andersbjuro@hotmail.com

Anonymous said...

Great. Can you send me the source code? hstoker@vxcompany.com

Anonymous said...

Your custom messagebox is really nice. Can I have the source code please? shortstory@free.fr

Anonymous said...

Your custom messagebox control looks really nice. Can I have the source code please? shortstory@free.fr

Shortstory said...

Your custom messagebox control looks really nice. Can I have the source code please? shortstory@free.fr

shortstory said...

Your custom messagebox control looks really nice. Can I have the source code please? shortstory@free.fr

shortstory said...

Your custom messagebox control looks really nice. Can I have the source code please? shortstory@free.fr

Anonymous said...

Hello

It looks nice, It would be very great if you send me the source code
Can I use it in a WPF Browser Application

Thanks

cvoisin1 said...

Very good job.
Looks excatly like what I am looking for !!!
Could I have the source code please ?

Thanks.

Prajeesh said...

Hi cvoisin,
Could you please leave your email id for source code?

Anonymous said...

Really nice your control. Could you send to me the source code, please. shortstory@free.fr

Anonymous said...

Could I get the source code please? crapmail234@gmail.com. Thanks!

Anonymous said...

may i have the source code as well

thanks alot!

slaked90@yahoo.com.sg

Anonymous said...

Very interesting article! Can I get the code at tedyusa2000@yahoo.ca

Happy New Year!

Anonymous said...

Good Work.

Can I please have the source code? My email address is minnimadan@gmail.com

Anonymous said...

Very nice work!
Could you send the source code to mah@texoapplication.se?

Regards and thanks!

Anonymous said...

Very nice work!

Could you please send the source code to mah@texoapplication.se?

Regards and thanks!

Anonymous said...

prof_hayalet@hotmail.com

thanks

Anonymous said...

Hi Prajeesh,

Nice work!

r0xz0rz@yahoo.com

Anonymous said...

Hi Prajeesh

Will you please mail me the source

vorstert@gmail.com

Thanks

Anonymous said...

This looks good. Can you please send me the source code
pintookhara@gmail.com

Anonymous said...

Thanks!

Emin said...

Really good job.
Could I have the source code please ?
emininam@gmail.com

Emin İnam
thanks

Faris said...

That's a very good idea.

Can you send me the source code please?
just.faris@gmail.com

Thanks.

Anonymous said...

Looks good, could you send me the source code ?

b0uncer@sapo.pt

thanks.

Anonymous said...

hi,

looks good, can you send me the sourcecode.

jikubhai@yahoo.com

thanks

Anonymous said...

Looks good, could you send me the source code ?

hlisa@walla.co.il

thanks.

Anonymous said...

Looks good, could you send me the source code ?

hlisa@walla.co.il

thanks.

Easy E said...

Hey, looks great.

Could I please get the Source Code?

ecairncross@gmail.com
Thanks

trialBoy said...

Looks great. Can I have the source code?

trialboy@live.cn

trialBoy said...

Looks great. Can I have the source code?

q2onq2@yahoo.com.cn

Thanks

Corneflex said...

Great job,
Can I get the source code.
corneflex@gmail.com

Simon said...

Him great control!
Could you please send the source code to simon(dot)wolters(at)gmx(dot)de?
Thank you!

PJ said...

Just what I was looking for, pljames mmm.com thanks!

sagar said...

got i was lookin for since 3 days...
thnx dude....
ur post has really helped me a lot....

juan manuel said...

That's exactly what I am looking for! Could you please send me the source files to pirlock34@hotmail.com?

Thanks in advance!!!

sagar said...

can u plz mail me d source code...
i dint get it the last time u sent...
plz mail it on sagardoshi_7891@yahoo.co.in....

Anonymous said...

This is what i am looking for Can you please send me the source code to shaikphakeer@yahoo.com

Thanks
Shaik

Anonymous said...

Prajeesh,
Please send me (cs.3.rich1954@spamgourmet.com) the source code. Thanks in advance!
-R Frainier

Kuldeep said...

Can you please send the source code to join10_2000@yahoo.com.

Anonymous said...

Could I please get the source code? Thanks.
~ eshoberg + @ + hotmail + . "com"

Anonymous said...

Please send source code to:

seaero@gmail.com

Thanks!

Anonymous said...

Please send source code to:

sam_mahanta@msn.com

Thanks in advance!

Anonymous said...

Hi, This looks like just what I'm looking for. May I have the source code please. Many Thanks in advance.

plumbja@yahoo.co.uk

Anonymous said...

Thanks for the offer - I'd like the source code too pls.

BTW, have you thought of posting the code up on CodeProject?

brettg@dodo.com.au

Anonymous said...

Nice try, but it would be a more helpful resource if you weren't asking people to put their email addresses up on your site to get your source code.

Anonymous said...

Hi,

i'm interresting having your code.

could you email me it at julien.roy@logica.com

thanks

Anonymous said...

Looks Great!
Possible if I could get a hand on the source :)

justinwong85@gmail.com

johannes g said...

hi. very nice work! could you please send me the source (kostakimu@googlemail.com)? thanks in advance..

Jelena said...

That look awesome!
Please, could you send me the src?

jelenastankov@gmail.com

Anonymous said...

Excellent post.

Please send me source code also.

Thanks very much.

guy.landingham@verizon.net

Anonymous said...

Looks great, really helpfull, could you send me the source code?


Thanks
yogesh_megan@hotmail.com

LaSerpe said...

I'd really love to take a look at the source code. Please, send me it at laserpe.z at gmail.com.

Thank you in advance!

krise said...

hi prajeesh, the control looks awesome. good job. Can I hav a look at source code.
and my mail Id is Vamsip.net@gmail.com

Alex said...

It looks very nice. Can I get it?

Anonymous said...

cool,
kiveendra@gmail.com

Anonymous said...

Excelent messagebox, i hope U can send me the source code... Thanks in advance...
cesarpjc79@hotmail.com

Anonymous said...

please send me the code to the following e-mail. ramanpk@hotmail.com

Frank said...

Hi,
I'm still waiting for the source code ? Thanks !

telecino@gmail.com

Anonymous said...

This what exactly I am searching thank you Prajeesh.Please mail source code to me at ravi.avpg@gmail.com

Daniil said...

Very good, please send source to
danshar@bk.ru

Anonymous said...

Very good, please send source to
danshar@bk.ru

Prajeesh said...

I have uploaded the source code to codeplex. You can get it from here
http://prajeeshprathap.codeplex.com/SourceControl/PatchList.aspx

Anonymous said...

Very Nice!!
Saved us hours of works!

Steve

Huy Ho said...

Thanks for sharing the code. While I was testing this, I notice something that is a bit inconsistent with the built in message box:
- I don't think you are handling the Escape key to close the window.
- And when I close the window using the 'X' button, the WPFMessageBoxResult is always default to Yes. Most of the time, this is okay, but I think it can lead to some expected results.