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:

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. Looks good, please could I have the src?

    chrisblanchard@gmx.co.uk

    thanks

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

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

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

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

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

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

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

    ReplyDelete
  11. 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

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

    Thanks.

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

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

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

    ReplyDelete
  16. may i have the source code as well

    thanks alot!

    slaked90@yahoo.com.sg

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

    Happy New Year!

    ReplyDelete
  18. Good Work.

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

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

    Regards and thanks!

    ReplyDelete
  20. Very nice work!

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

    Regards and thanks!

    ReplyDelete
  21. Hi Prajeesh,

    Nice work!

    r0xz0rz@yahoo.com

    ReplyDelete
  22. Hi Prajeesh

    Will you please mail me the source

    vorstert@gmail.com

    Thanks

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

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

    Emin İnam
    thanks

    ReplyDelete
  25. That's a very good idea.

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

    Thanks.

    ReplyDelete
  26. Looks good, could you send me the source code ?

    b0uncer@sapo.pt

    thanks.

    ReplyDelete
  27. hi,

    looks good, can you send me the sourcecode.

    jikubhai@yahoo.com

    thanks

    ReplyDelete
  28. Looks good, could you send me the source code ?

    hlisa@walla.co.il

    thanks.

    ReplyDelete
  29. Looks good, could you send me the source code ?

    hlisa@walla.co.il

    thanks.

    ReplyDelete
  30. Hey, looks great.

    Could I please get the Source Code?

    ecairncross@gmail.com
    Thanks

    ReplyDelete
  31. Looks great. Can I have the source code?

    trialboy@live.cn

    ReplyDelete
  32. Looks great. Can I have the source code?

    q2onq2@yahoo.com.cn

    Thanks

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

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

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

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

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

    Thanks in advance!!!

    ReplyDelete
  38. 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....

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

    Thanks
    Shaik

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

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

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

    ReplyDelete
  43. Please send source code to:

    seaero@gmail.com

    Thanks!

    ReplyDelete
  44. Please send source code to:

    sam_mahanta@msn.com

    Thanks in advance!

    ReplyDelete
  45. 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

    ReplyDelete
  46. 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

    ReplyDelete
  47. 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.

    ReplyDelete
  48. Hi,

    i'm interresting having your code.

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

    thanks

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

    justinwong85@gmail.com

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

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

    jelenastankov@gmail.com

    ReplyDelete
  52. Excellent post.

    Please send me source code also.

    Thanks very much.

    guy.landingham@verizon.net

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


    Thanks
    yogesh_megan@hotmail.com

    ReplyDelete
  54. 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!

    ReplyDelete
  55. 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

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

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

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

    telecino@gmail.com

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

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

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

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

    ReplyDelete
  63. Very Nice!!
    Saved us hours of works!

    Steve

    ReplyDelete
  64. 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.

    ReplyDelete
  65. Your custom messagebox control looks really nice. Can I have the source code please?

    gopalrmahale@gmail.com

    ReplyDelete
  66. Thanks You. Nice to hear that you've found the post useful.

    ReplyDelete