65.9K
CodeProject is changing. Read more.
Home

TIP: Handling WPF DialogResult with simplicity

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.89/5 (8 votes)

Jun 11, 2010

CPOL
viewsIcon

37960

Shorter way to evaluate DialogResult after calling Window.ShowDialog

I have seen many code snippets like:
 
frm.ShowDialog();
if (frm.DialogResult.HasValue && frm.DialogResult.Value) 
{
   // do something here
}
or
DialogResult result = frm.ShowDialog();
if (result.HasValue && result.Value)
{
   // do something here
}
This double check is done because DialogResult is of nullable type: bool?, but evaluation can be as simple as:
frm.ShowDialog();
if (frm.DialogResult.Equals(true))
{
   // do something here
}