Cannot implicitly convert from dialog result to bool

I am 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 get a compilation error

Can't implicitly convert type 'System.Windows.Forms.DialogResult' to 'BOOL?

+4
source share
2 answers

change it to

 return (new BCustomerSettingsDialog()).ShowDialog() == DialogResult.OK; 
+11
source

The reason is that in Windows Forms, the ShowDialog method returns DialogResult . The range of possible values ​​depends on which buttons you have available and their conversion bool? may depend on what they mean in your application. The following is some general logic for handling multiple 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.") } } } 
+7
source

Source: https://habr.com/ru/post/1490870/


All Articles