dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException ("button", (int)button…
Error message
InvalidEnumArgumentException ("button", (int)button, typeof(MessageBoxButton)) What it means
MessageBox.ShowCore validates that the button parameter is a defined MessageBoxButton enum value before showing the dialog. An out-of-range or undefined value raises InvalidEnumArgumentException naming parameter "button" and type MessageBoxButton.
Solutions
- Pass a defined MessageBoxButton value: OK, OKCancel, YesNo, or YesNoCancel.
- Validate external values with Enum.IsDefined(typeof(MessageBoxButton), value) before calling Show.
- Check argument order in the Show overload being used to ensure button/icon/defaultResult/options weren't swapped.
Example fix
// before var b = (MessageBoxButton)cfg.ButtonInt; // may be undefined MessageBox.Show(msg, title, b); // after var b = Enum.IsDefined(typeof(MessageBoxButton), cfg.ButtonInt) ? (MessageBoxButton)cfg.ButtonInt : MessageBoxButton.OK; MessageBox.Show(msg, title, b);
Defensive patterns
Strategy: validation
Validate before calling
bool valid = Enum.IsDefined(typeof(MessageBoxButton), button); if (!valid) throw new ArgumentOutOfRangeException(nameof(button));
Type guard
bool IsValidButton(MessageBoxButton b) => b is MessageBoxButton.OK or MessageBoxButton.OKCancel or MessageBoxButton.YesNo or MessageBoxButton.YesNoCancel;
Try / catch
try { MessageBox.Show(msg, title, button); }
catch (InvalidEnumArgumentException ex) when (ex.ParamName == "button") { MessageBox.Show(msg, title, MessageBoxButton.OK); } Prevention
- Validate config-stored button ints with Enum.IsDefined
- Never swap the enum parameters in Show overloads
- Use named enum members only
When it happens
Trigger: Calling any MessageBox.Show overload with a button argument cast from an invalid int, e.g. (MessageBoxButton)7, or passing a MessageBoxImage/other enum by mistake.
Common situations: Reading the button style from config or a database int column; mapping a Win32 MB_* constant that is out of the MessageBoxButton range; copy-paste swapping parameter order in a Show overload with many enums.
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 ("defaultResult"…
- InvalidEnumArgumentException ("icon", (int)icon…
- InvalidEnumArgumentException("options", (int)options…
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(userActivationMode)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/80e8834767956a03.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs:372
if (result == MessageBoxResult.Continue) return DEFAULT_BUTTON3;
return DEFAULT_BUTTON1;
default:
return DEFAULT_BUTTON1;
}
}
internal static MessageBoxResult ShowCore(
IntPtr owner,
string messageBoxText,
string caption,
MessageBoxButton button,
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)");View on GitHub (pinned to 81131a70a4)