MisterXero
MisterXero

Reputation: 1128

System.Windows.MessageBox vs System.Windows.Forms.MessageBox

I am having trouble finding out what the key differences are between the two message boxes. What is the difference between System.Windows.MessageBox and System.Windows.Forms.MessageBox?

Upvotes: 30

Views: 17629

Answers (4)

Stacy Dudovitz
Stacy Dudovitz

Reputation: 1041

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

Upvotes: 13

Alex McBride
Alex McBride

Reputation: 7011

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.

Upvotes: 2

Felice Pollano
Felice Pollano

Reputation: 33252

Both eventually call the same low level windows API as far as I know...

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564413

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.

Upvotes: 35

Related Questions