System.Windows.MessageBox vs System.Windows.Forms.MessageBox
Solution 1
System.Windows.MessageBox
was added with WPF, and exists within the WPF assemblies (PresentationFramework.dll).
System.Windows.Forms.MessageBox
was added with Windows Forms, and exists within the Windows Forms assemblies.
If your program is a Windows Forms program, I would use the latter (System.Windows.Forms.MessageBox
), as it won't pull in a dependency on WPF. On the other hand, if you are developing for WPF, I'd use System.Windows.MessageBox
.
Solution 2
One additional point should be noted:
If you want to display a message box in an application that is neither a windows forms application or a forms application (such as a .NET console application), you should not drag in assembly references for either as seems to be the common mantra all over the internet.
Instead, you should use pinvoke and call into User32 as follows:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);
/// <summary>
/// Flags that define appearance and behaviour of a standard message box displayed by a call to the MessageBox function.
/// </summary>
[Flags]
public enum MessageBoxOptions : uint
{
Ok = 0x000000,
OkCancel = 0x000001,
AbortRetryIgnore = 0x000002,
YesNoCancel = 0x000003,
YesNo = 0x000004,
RetryCancel = 0x000005,
CancelTryContinue = 0x000006,
IconHand = 0x000010,
IconQuestion = 0x000020,
IconExclamation = 0x000030,
IconAsterisk = 0x000040,
UserIcon = 0x000080,
IconWarning = IconExclamation,
IconError = IconHand,
IconInformation = IconAsterisk,
IconStop = IconHand,
DefButton1 = 0x000000,
DefButton2 = 0x000100,
DefButton3 = 0x000200,
DefButton4 = 0x000300,
ApplicationModal = 0x000000,
SystemModal = 0x001000,
TaskModal = 0x002000,
Help = 0x004000, //Help Button
NoFocus = 0x008000,
SetForeground = 0x010000,
DefaultDesktopOnly = 0x020000,
Topmost = 0x040000,
Right = 0x080000,
RTLReading = 0x100000,
}
/// <summary>
/// Represents possible values returned by the MessageBox function.
/// </summary>
public enum MessageBoxResult : uint
{
Ok = 1,
Cancel,
Abort,
Retry,
Ignore,
Yes,
No,
Close,
Help,
TryAgain,
Continue,
Timeout = 32000
}
var result = User32.MessageBox(IntPtr.Zero, "Debugging Break", "Your Console Application", (int)User32.MessageBoxOptions.Ok);
Solution 3
Both eventually call the same low level windows API as far as I know...
Solution 4
The both basically do the same thing, except system.windows.messagebox
is WPF and system.windows.forms.messagebox
is Windows Forms. If you're using WPF use the former, for WinForms use the latter.
Related videos on Youtube
MisterXero
Updated on May 15, 2022Comments
-
MisterXero almost 2 years
I am having trouble finding out what the key differences are between the two message boxes. What is the difference between
System.Windows.MessageBox
andSystem.Windows.Forms.MessageBox
?-
user1055201 about 13 yearsYou mention twice, in comments, that one message box "breaks your bindings" can you clarify, or have you, as suggested, started another question on that topic?
-
-
MisterXero over 13 yearsInteresting...When I use system.windows.messagebox it breaks a binding on my WPF form but the system.windows.forms.messagebox does not. Any ideas why it might be doing that?
-
MisterXero over 13 yearsThanks for the answer! I am using a WPF form and one of the reasons I was curious about this was when I used System.Windows.MessageBox it was causing some of my bindings to break. When I switched over to System.Windows.Forms.MessageBox my bindings were left intact. Are there certain implementation aspects of the MessageBox that may be breaking my bindings? Thanks
-
Reed Copsey over 13 years@MisterXero: No - it shouldn't have any impact at all. You might want to ask another question explaining what you're doing (with code samples) for that...
-
jrwren over 12 yearsJFYI: System.Windows.MessageBox from WPF Presentation Framework is just a managed wrapper around this call. WPF MessageBox is not actually a WPF window. So if you are having reservations about doing what @stacy suggests, don't have those reservations.
-
mbx almost 11 years@MisterXero what exactly do you mean with "breaks a binding"? Adding an assembly reference und using the full name shouldn't break anything. Both have an internal
UnsafeNativeMethods
class in different namespaces doing about the same, although the WPF implementation is more elegant.