dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException ("defaultResult"…
Error message
InvalidEnumArgumentException ("defaultResult", (int)defaultResult, typeof(MessageBoxResult)) What it means
MessageBox.ShowCore validates that defaultResult is a defined MessageBoxResult value (None, OK, Cancel, Yes, No). This parameter sets the dialog's default button; an undefined value raises InvalidEnumArgumentException naming parameter "defaultResult".
Solutions
- Pass a defined MessageBoxResult value: None, OK, Cancel, Yes, or No.
- Validate external values with Enum.IsDefined(typeof(MessageBoxResult), value) before calling Show.
- Match defaultResult to the button set: e.g. don't use MessageBoxResult.Yes with MessageBoxButton.OKCancel.
Example fix
// before MessageBox.Show(msg, title, MessageBoxButton.YesNo, MessageBoxImage.Question, (MessageBoxResult)lastInt); // after var def = Enum.IsDefined(typeof(MessageBoxResult), lastInt) && lastInt != (int)MessageBoxResult.None ? (MessageBoxResult)lastInt : MessageBoxResult.No; MessageBox.Show(msg, title, MessageBoxButton.YesNo, MessageBoxImage.Question, def);
Defensive patterns
Strategy: validation
Validate before calling
bool valid = Enum.IsDefined(typeof(MessageBoxResult), defaultResult); if (!valid) defaultResult = MessageBoxResult.None;
Type guard
bool IsValidResult(MessageBoxResult r) => r is MessageBoxResult.None or MessageBoxResult.OK or MessageBoxResult.Cancel or MessageBoxResult.Yes or MessageBoxResult.No;
Try / catch
try { MessageBox.Show(msg, title, button, icon, defaultResult); }
catch (InvalidEnumArgumentException ex) when (ex.ParamName == "defaultResult") { MessageBox.Show(msg, title, button, icon, MessageBoxResult.None); } Prevention
- Keep MessageBoxResult and MessageBoxButton enums separate
- Make defaultResult consistent with the button set (e.g. Yes only with YesNo*)
- Validate persisted results before reusing them
When it happens
Trigger: Calling a MessageBox.Show overload that takes a defaultResult with an undefined value, e.g. (MessageBoxResult)99 from a config int, or passing a MessageBoxButton/other enum in the defaultResult slot.
Common situations: Persisting last-used dialog result and feeding it back as default; mixing up MessageBoxResult with MessageBoxButton (both small enums); computing the value from a switch with a bad default.
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 ("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/a7ce173026ffde59.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs:380
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)");
}*/
if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{
if (owner != IntPtr.Zero)
{
throw new ArgumentException(SR.CantShowMBServiceWithOwner);
}View on GitHub (pinned to 81131a70a4)