Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert from dialog result to bool

Tags:

c#

.net

winforms

I'm trying to reproduce code from wpf to winforms (this code works inside wpf)

public static bool? ShowSettingsDialogFor(ICustomCustomer)
{
   if (cust is BasicCustomer)
   {
      return (new BCustomerSettingsDialog()).ShowDialog();
   }
}

I'm getting compile error message

Cannot implicitly convert type 'System.Windows.Forms.DialogResult' to 'bool?'

like image 551
panjo Avatar asked Jul 11 '13 09:07

panjo


2 Answers

change that to

return (new BCustomerSettingsDialog()).ShowDialog() == DialogResult.OK;
like image 139
Antonio Bakula Avatar answered Nov 15 '22 20:11

Antonio Bakula


The reason is that, in Windows Forms, the ShowDialog method returns a DialogResult enumeration value. The range of possible values depends on which buttons you have available, and their bool? conversion may depend on what they imply within your application. Below is some generic logic to handle a few cases:

public static bool? ShowSettingsDialogFor(ICustomCustomer)
{
   if (cust is BasicCustomer)
   {
      DialogResult result = (new BCustomerSettingsDialog()).ShowDialog();

      switch (result)
      {
          case DialogResult.OK:
          case DialogResult.Yes:
              return true;

          case DialogResult.No:
          case DialogResult.Abort:
              return false;

          case DialogResult.None:
          case DialogResult.Cancel:
              return null;

          default:
              throw new ApplicationException("Unexpected dialog result.")
      }
   }
}
like image 29
Douglas Avatar answered Nov 15 '22 19:11

Douglas