{"record":{"id":"735154da089204db","repo":"dotnet/wpf","slug":"invalidenumargumentexception-icon-int-icon-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException (\"icon\", (int)icon, typeof(MessageBoxImage))","messagePattern":"InvalidEnumArgumentException \\(\"icon\", \\(int\\)icon, typeof\\(MessageBoxImage\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs","lineNumber":376,"sourceCode":"            }\n        }\n\n        internal static MessageBoxResult ShowCore(\n            IntPtr owner,\n            string messageBoxText,\n            string caption,\n            MessageBoxButton button,\n            MessageBoxImage icon,\n            MessageBoxResult defaultResult,\n            MessageBoxOptions options)\n        {\n            if (!IsValidMessageBoxButton(button))\n            {\n                throw new InvalidEnumArgumentException (\"button\", (int)button, typeof(MessageBoxButton));\n            }\n            if (!IsValidMessageBoxImage(icon))\n            {\n                throw new InvalidEnumArgumentException (\"icon\", (int)icon, typeof(MessageBoxImage));\n            }\n            if (!IsValidMessageBoxResult(defaultResult))\n            {\n                throw new InvalidEnumArgumentException (\"defaultResult\", (int)defaultResult, typeof(MessageBoxResult));\n            }\n            if (!IsValidMessageBoxOptions(options))\n            {\n                throw new InvalidEnumArgumentException(\"options\", (int)options, typeof(MessageBoxOptions));\n            }\n\n            // UserInteractive??\n            //\n            /*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {\n                throw new InvalidOperationException(\"UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)\");\n            }*/\n\n            if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)\n            {","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs#L358-L394","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a defined MessageBoxImage value: None, Error, Question, Warning, Information (or their aliases Hand, Asterisk, Exclamation).","Validate any externally sourced int with Enum.IsDefined(typeof(MessageBoxImage), value) first.","Verify the Show overload's parameter order so the icon slot receives the icon, not the button enum."],"exampleFix":"// before\nMessageBox.Show(msg, title, MessageBoxButton.OK, (MessageBoxImage)iconInt);\n// after\nvar icon = Enum.IsDefined(typeof(MessageBoxImage), iconInt) ? (MessageBoxImage)iconInt : MessageBoxImage.None;\nMessageBox.Show(msg, title, MessageBoxButton.OK, icon);","handlingStrategy":"validation","validationCode":"bool valid = Enum.IsDefined(typeof(MessageBoxImage), icon);\nif (!valid) icon = MessageBoxImage.None;","typeGuard":"bool IsValidIcon(MessageBoxImage i) => i is MessageBoxImage.None or MessageBoxImage.Error or MessageBoxImage.Question or MessageBoxImage.Warning or MessageBoxImage.Information;","tryCatchPattern":"try { MessageBox.Show(msg, title, button, icon); }\ncatch (InvalidEnumArgumentException ex) when (ex.ParamName == \"icon\") { MessageBox.Show(msg, title, button, MessageBoxImage.None); }","preventionTips":["Map Win32 MB_ICON constants explicitly, don't cast them directly","Validate stored icon ints before use","Keep the Show argument order (button, icon, defaultResult, options) memorized or use named overloads"],"tags":["wpf","messagebox","enum","argument-validation"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}