{"record":{"id":"31678170c83af158","repo":"dotnet/wpf","slug":"invalidenumargumentexception-options-int-options-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"options\", (int)options, typeof(MessageBoxOptions))","messagePattern":"InvalidEnumArgumentException\\(\"options\", \\(int\\)options, typeof\\(MessageBoxOptions\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs","lineNumber":384,"sourceCode":"            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            {\n                if (owner != IntPtr.Zero)\n                {\n                    throw new ArgumentException(SR.CantShowMBServiceWithOwner);\n                }\n            }\n            else\n            {\n                if (owner == IntPtr.Zero)","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs#L366-L402","documentation":"MessageBox.ShowCore validates that options is a defined MessageBoxOptions value or a valid combination of its flags. An undefined value or combination raises InvalidEnumArgumentException naming parameter \"options\" and type MessageBoxOptions.","triggerScenarios":"Calling MessageBox.Show with an options argument that is not a valid MessageBoxOptions (RightAlign, RtlReading, DefaultDesktopOnly, ServiceNotification) combination, e.g. (MessageBoxOptions)0x400 from raw Win32 flags.","commonSituations":"Porting Win32 MessageBox flag constants directly; combining options bitwise with unrelated flags; building options from stored int settings.","solutions":["Pass only MessageBoxOptions-defined flags: DefaultDesktopOnly, RightAlign, RtlReading, ServiceNotification (or a valid combination of these).","Mask/validate stored ints against the enum's defined flags before passing, e.g. Enum.IsDefined or a flags-range check.","Avoid raw Win32 constants; map them explicitly to MessageBoxOptions members."],"exampleFix":"// before\nvar opt = (MessageBoxOptions)win32Flags; // arbitrary Win32 bits\nMessageBox.Show(msg, title, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, opt);\n// after\nvar opt = MessageBoxOptions.None;\nif ((win32Flags & 0x80000) != 0) opt |= MessageBoxOptions.RightAlign;\nif ((win32Flags & 0x100000) != 0) opt |= MessageBoxOptions.RtlReading;\nMessageBox.Show(msg, title, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, opt);","handlingStrategy":"validation","validationCode":"const MessageBoxOptions ValidFlags = MessageBoxOptions.DefaultDesktopOnly | MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading | MessageBoxOptions.ServiceNotification;\nbool valid = (options & ~ValidFlags) == 0;\nif (!valid) options = MessageBoxOptions.None;","typeGuard":"bool IsValidOptions(MessageBoxOptions o) => (o & ~(MessageBoxOptions.DefaultDesktopOnly | MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading | MessageBoxOptions.ServiceNotification)) == 0;","tryCatchPattern":"try { MessageBox.Show(msg, title, button, icon, result, options); }\ncatch (InvalidEnumArgumentException ex) when (ex.ParamName == \"options\") { MessageBox.Show(msg, title, button, icon, result, MessageBoxOptions.None); }","preventionTips":["Build options from named flags, never from raw Win32 ints","Mask stored flag ints against the enum's defined bits","Remember only these four flags exist: DefaultDesktopOnly, RightAlign, RtlReading, ServiceNotification"],"tags":["wpf","messagebox","enum","flags","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"}