dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException ("icon", (int)icon…

Error message

InvalidEnumArgumentException ("icon", (int)icon, typeof(MessageBoxImage))

What it means

MessageBox.ShowCore validates that the icon parameter is a defined MessageBoxImage value (None, Error, Question, Warning, Information plus hand/asterisk/exclamation aliases). An undefined value raises InvalidEnumArgumentException naming parameter "icon" and type MessageBoxImage.

Solutions

  1. Pass a defined MessageBoxImage value: None, Error, Question, Warning, Information (or their aliases Hand, Asterisk, Exclamation).
  2. Validate any externally sourced int with Enum.IsDefined(typeof(MessageBoxImage), value) first.
  3. Verify the Show overload's parameter order so the icon slot receives the icon, not the button enum.

Example fix

// before
MessageBox.Show(msg, title, MessageBoxButton.OK, (MessageBoxImage)iconInt);
// after
var icon = Enum.IsDefined(typeof(MessageBoxImage), iconInt) ? (MessageBoxImage)iconInt : MessageBoxImage.None;
MessageBox.Show(msg, title, MessageBoxButton.OK, icon);
Defensive patterns

Strategy: validation

Validate before calling

bool valid = Enum.IsDefined(typeof(MessageBoxImage), icon);
if (!valid) icon = MessageBoxImage.None;

Type guard

bool IsValidIcon(MessageBoxImage i) => i is MessageBoxImage.None or MessageBoxImage.Error or MessageBoxImage.Question or MessageBoxImage.Warning or MessageBoxImage.Information;

Try / catch

try { MessageBox.Show(msg, title, button, icon); }
catch (InvalidEnumArgumentException ex) when (ex.ParamName == "icon") { MessageBox.Show(msg, title, button, MessageBoxImage.None); }

Prevention

When it happens

Trigger: Calling MessageBox.Show with an icon argument that is an undefined enum value, e.g. a raw int from storage like (MessageBoxImage)42, or accidentally passing the MessageBoxButton enum where an icon is expected.

Common situations: Mapping user preferences stored as ints to dialog icons; Win32 icon constant (e.g. MB_ICON*) values that don't map 1:1 to MessageBoxImage; parameter-order swap in a Show overload.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/735154da089204db. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs:376

            }
        }

        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)");
            }*/

            if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
            {

View on GitHub (pinned to 81131a70a4)