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?'
change that to
return (new BCustomerSettingsDialog()).ShowDialog() == DialogResult.OK;
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.")
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With