WPF Sample Series – Custom Dialog (Vista and XP)

Sample Updated on 12/30/2007 

This article was awarded the Code Project VB.NET Article Award Dec 2007.

This is the next sample in the WPF Sample Applications Series.  The purpose of the Sample Series is to provide concise code solutions for specific programming tasks.  This sample provides a brief description of the problem, the solution and full source code with a test bench program.

This sample covers a very important topic, dialog boxes.  With the release of Vista also came the cool TaskDialog that all Vista users are getting used to.  It provides a richer dialog than the standard message box dialog available on Windows XP. 

However, this great new feature causes a minor bump in the carpet for WPF developers.   Which dialog should WPF developers use?  Should WPF developers customize their dialogs for each operating system?  What about application documentation?  (Can you image having to document two flavors of every dialog box.)  One solution clearly provides for much more information than the other, so what to do?

This is the very question that tapped on my shoulder about three months ago.  So I developed a custom dialog class that works on both Vista and XP.  It looks just like Vista’s TaskDialog, yet without all the messy API business.  (Micrsoft, please add TaskDialog to the 3.6 Framework so we WPF developers do not need to fool with the Windows API.)  If you’re interested, you can have a look at the VistaBridge application in the Windows SDK.

Note
After I initially posted this Sample, I had way more views and downloads of this post than I expected. So I have expanded this post to an article on Code Project. The article there are class diagrams and a few areas have been expanded.

The Code project article can be view here:
http://www.codeproject.com/KB/WPF/WPFTaskDialogVistaAndXP.aspx

Sample Program

Sample Program

Sample One (Vista)

Sample One

Sample One (Windows XP)

 xpsample

Sample Two

Sample Two

Sample Three

Sample Three

Features

  • Same simple interface for WPF developers.  Works the same on Vista and XP – simplifies programming
  • Common icons and text for standard documentation for Vista and XP
  • Rich dialog box capabilities
  • Detailed user dialog usage logging capabilities built in. (see TODO in project)
  • Optional delay feature
  • Dialog box must be closed by clicking on a button.  (ALT-F4 is also disabled)

Usage Example

  Dim obj As New CommonDialog.CustomDialog
  obj.AdditionalDetailsText = “These are additional details”
  obj.Buttons = CommonDialog.CustomDialogButtons.OKCancel
  obj.ButtonsDisabledDelay = 5
  obj.Caption = “The buttons are disabled for 5 seconds”
  obj.DefaultButton = CommonDialog.CustomDialogResults.OK
  obj.FooterIcon = CommonDialog.CustomDialogIcons.Information
  obj.FooterText = “This is the footer text”
  obj.InstructionHeading = “This is the instruction heading”
  obj.InstructionIcon = CommonDialog.CustomDialogIcons.Question
  obj.InstructionText = “Do you want to proceed with the next task?”
  Dim objResults As CommonDialog.CustomDialogResults = obj.ShowMe.tbResults.Text = String.Format(“Last dialog result was {0}”, objResults.ToString)

Let me touch on the optional delay feature.  Many times our beloved users are quick on the keyboard or mouse and just click through important dialog boxes.  They tend to tell customer support, “I never saw that box!”  For those users ( I can smell a new user profile setting ), or for very important dialog boxes, I added the delay property. 

Just assign how many seconds you want the buttons to be disabled before the user can respond to the dialog box.  Since this dialog can’t be closed with the little red X or by pressing ALT-F4, your user will have plenty of time to actually read and digest the information you are trying to communicate.  Just don’t get to crazy with the delay feature.

The logging feature is also very cool.  Get into the source code and check the project TODO comment.  This custom dialog has many properties that can be logged including stack trace information so that developers and support staff can easily determine who, what, where, when and why dialog information. 

Resources: I am the original author for all this code.

Source Code: After downloading the source code you MUST change the file extension from .zip.DOC to .zip. This is a requirment of WordPress.com.

Download Source Code 104KB (Updated 12/30/2007)

Hope you can learn just a little bit more about WPF from the Sample Series.

Just a grain of sand on the worlds beaches.

Leave a Reply

You must be logged in to post a comment.