dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("options", (int)options…
Error message
InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions)) What it means
MessageBox.ShowCore validates that options is a defined MessageBoxOptions value or a valid combination of its flags. An undefined value or combination raises InvalidEnumArgumentException naming parameter "options" and type MessageBoxOptions.
Solutions
- Pass only MessageBoxOptions-defined flags: DefaultDesktopOnly, RightAlign, RtlReading, ServiceNotification (or a valid combination of these).
- Mask/validate stored ints against the enum's defined flags before passing, e.g. Enum.IsDefined or a flags-range check.
- Avoid raw Win32 constants; map them explicitly to MessageBoxOptions members.
Example fix
// before var opt = (MessageBoxOptions)win32Flags; // arbitrary Win32 bits MessageBox.Show(msg, title, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, opt); // after var opt = MessageBoxOptions.None; if ((win32Flags & 0x80000) != 0) opt |= MessageBoxOptions.RightAlign; if ((win32Flags & 0x100000) != 0) opt |= MessageBoxOptions.RtlReading; MessageBox.Show(msg, title, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, opt);
Defensive patterns
Strategy: validation
Validate before calling
const MessageBoxOptions ValidFlags = MessageBoxOptions.DefaultDesktopOnly | MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading | MessageBoxOptions.ServiceNotification; bool valid = (options & ~ValidFlags) == 0; if (!valid) options = MessageBoxOptions.None;
Type guard
bool IsValidOptions(MessageBoxOptions o) => (o & ~(MessageBoxOptions.DefaultDesktopOnly | MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading | MessageBoxOptions.ServiceNotification)) == 0;
Try / catch
try { MessageBox.Show(msg, title, button, icon, result, options); }
catch (InvalidEnumArgumentException ex) when (ex.ParamName == "options") { MessageBox.Show(msg, title, button, icon, result, MessageBoxOptions.None); } Prevention
- Build options from named flags, never from raw Win32 ints
- Mask stored flag ints against the enum's defined bits
- Remember only these four flags exist: DefaultDesktopOnly, RightAlign, RtlReading, ServiceNotification
When it happens
Trigger: Calling MessageBox.Show with an options argument that is not a valid MessageBoxOptions (RightAlign, RtlReading, DefaultDesktopOnly, ServiceNotification) combination, e.g. (MessageBoxOptions)0x400 from raw Win32 flags.
Common situations: Porting Win32 MessageBox flag constants directly; combining options bitwise with unrelated flags; building options from stored int settings.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- InvalidEnumArgumentException ("button", (int)button…
- InvalidEnumArgumentException ("defaultResult"…
- InvalidEnumArgumentException ("icon", (int)icon…
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(userActivationMode)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/31678170c83af158.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs:384
MessageBoxImage icon,
MessageBoxResult defaultResult,
MessageBoxOptions options)
{
if (!IsValidMessageBoxButton(button))
{
throw new InvalidEnumArgumentException ("button", (int)button, typeof(MessageBoxButton));
}
if (!IsValidMessageBoxImage(icon))
{
throw new InvalidEnumArgumentException ("icon", (int)icon, typeof(MessageBoxImage));
}
if (!IsValidMessageBoxResult(defaultResult))
{
throw new InvalidEnumArgumentException ("defaultResult", (int)defaultResult, typeof(MessageBoxResult));
}
if (!IsValidMessageBoxOptions(options))
{
throw new InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions));
}
// UserInteractive??
//
/*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {
throw new InvalidOperationException("UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)");
}*/
if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{
if (owner != IntPtr.Zero)
{
throw new ArgumentException(SR.CantShowMBServiceWithOwner);
}
}
else
{
if (owner == IntPtr.Zero)View on GitHub (pinned to 81131a70a4)