1

I've run into this problem a lot lately. I often write programs using WPF/C# and want to use an OpenFileDialog or some other class requiring System.Windows.Forms. I also frequently use MessageBoxes (They become ambigious between the two libraries when Forms is added).

I add the reference, but (even with the shortcut) I have to use something like F.MessageBox.Show(). Is there a way to just temporarily use the System.Windows.Forms reference when I need or something else that will allow me to keep MessageBox.Show() intact?

Wilson
  • 8,570
  • 20
  • 66
  • 101
  • Possible duplicate of [System.Windows.MessageBox vs System.Windows.Forms.MessageBox](http://stackoverflow.com/questions/4660587/system-windows-messagebox-vs-system-windows-forms-messagebox) – Jim Fell May 17 '16 at 18:19

3 Answers3

3

Microsoft moved a lot of Windows dialogs to the Microsoft.Win32 namespace so you don't need to add a reference to all of System.Windows.Forms.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • Right. For completeness, [`Microsoft.Win32.OpenFileDialog`](http://msdn.microsoft.com/en-us/library/microsoft.win32.openfiledialog.aspx) is part of PresentationFramework.dll, so a WPF project *should* already have a reference to it, but if it's removed for whatever reason, that's the one to add. –  Mar 04 '13 at 19:55
2

You can place the following using directive on top of your files that need WinForms MessageBox:

using MessageBox = System.Windows.Forms.MessageBox;

That will resolve tha ambiguity

Sasha
  • 8,537
  • 4
  • 49
  • 76
0

I would recommend you use the equivalent that is offered by wpf. This is System.Windows.MessageBox. It almost looks like the one in windows forms, however the parameters and return values may differ. Below is a sample to illustrate it's use.

var res = MessageBox.Show("Shutdown now ?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (res== MessageBoxResult.Yes)
{
    Application.Current.Shutdown();
}
Thulani Chivandikwa
  • 3,402
  • 30
  • 33
  • Your answer is certainly in the right spirit, but I think the OP is already using the WPF `MessageBox`, but the WinForms `OpenFileDialog`. Moozhe's answer gives the WPF alternative for that. –  Mar 04 '13 at 19:59