41

I want to show confirmation Box in C# code. I've seen above solution for that but it shows me exception at 'Yes' as 'System.Nullable' does not contain definition for 'Yes'. How should I remove this error?

 private void listBox1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (sender is ListBoxItem)
        {
            ListBoxItem item = (ListBoxItem)sender;
            Harvest_TimeSheetEntry entryToDelete = (Harvest_TimeSheetEntry)item.DataContext;

            DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Are you sure?", "Delete Confirmation", MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)  // error is here
            {
                Globals._globalController.harvestManager.deleteHarvestEntry(entryToDelete);
            }
            else
            {
                System.Windows.MessageBox.Show("Delete operation Terminated");
            }

        }
    }
user2622971
  • 685
  • 3
  • 10
  • 20

1 Answers1

144

Instead of using WinForm MessageBox, use the MessageBox provided by WPF and later use MessageBoxResult instead of DialogResult in WPF.

like:

MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
        if (messageBoxResult == MessageBoxResult.Yes)
 //...........
Habib
  • 219,104
  • 29
  • 407
  • 436
  • and how to use it ? – The Doctor Feb 20 '17 at 23:46
  • edit your example, there are different types, edit `DialogResult messageBoxResult = ...` to `MessageBoxResult messageBoxResult = ...` or the opposite and in the if clause should be `if (messageBoxResult == MessageBoxResult.Yes)` – vinsa Jan 25 '18 at 15:40
  • @vinsa, thanks for pointing it out. Somehow it was edited wrong and that edit was approved as well. I have since rollback the edit. – Habib Jan 25 '18 at 15:48