Confirmation Box in C# wpf

user2622971 picture user2622971 · Aug 19, 2013 · Viewed 64.6k times · Source

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");
            }

        }
    }

Answer

Habib picture Habib · Aug 19, 2013

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)
 //...........