{"record":{"id":"80e8834767956a03","repo":"dotnet/wpf","slug":"invalidenumargumentexception-button-int-button-typeof-80e883","errorCode":null,"errorMessage":"InvalidEnumArgumentException (\"button\", (int)button, typeof(MessageBoxButton))","messagePattern":"InvalidEnumArgumentException \\(\"button\", \\(int\\)button, typeof\\(MessageBoxButton\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs","lineNumber":372,"sourceCode":"                    if (result == MessageBoxResult.Continue) return DEFAULT_BUTTON3;\n                    return DEFAULT_BUTTON1;\n                default:\n                    return DEFAULT_BUTTON1;\n            }\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)\");","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs#L354-L390","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar b = (MessageBoxButton)cfg.ButtonInt; // may be undefined\nMessageBox.Show(msg, title, b);\n// after\nvar b = Enum.IsDefined(typeof(MessageBoxButton), cfg.ButtonInt) ? (MessageBoxButton)cfg.ButtonInt : MessageBoxButton.OK;\nMessageBox.Show(msg, title, b);","handlingStrategy":"validation","validationCode":"bool valid = Enum.IsDefined(typeof(MessageBoxButton), button);\nif (!valid) throw new ArgumentOutOfRangeException(nameof(button));","typeGuard":"bool IsValidButton(MessageBoxButton b) => b is MessageBoxButton.OK or MessageBoxButton.OKCancel or MessageBoxButton.YesNo or MessageBoxButton.YesNoCancel;","tryCatchPattern":"try { MessageBox.Show(msg, title, button); }\ncatch (InvalidEnumArgumentException ex) when (ex.ParamName == \"button\") { MessageBox.Show(msg, title, MessageBoxButton.OK); }","preventionTips":["Validate config-stored button ints with Enum.IsDefined","Never swap the enum parameters in Show overloads","Use named enum members only"],"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-22T01:17:13.364Z"}