{"record":{"id":"c2cba1762260fbb9","repo":"dotnet/wpf","slug":"invalidenumargumentexception-button-int-button-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"button\", (int)button, typeof(MouseButton))","messagePattern":"InvalidEnumArgumentException\\(\"button\", \\(int\\)button, typeof\\(MouseButton\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseButton.cs","lineNumber":75,"sourceCode":"        /// <summary>\n        ///     Ensures MouseButton is set to a valid value.\n        /// </summary>\n        /// <remarks>\n        ///     There is a proscription against using Enum.IsDefined().  (it is slow)\n        ///     So we manually validate using a switch statement.\n        /// </remarks>\n        internal static void Validate(MouseButton button)\n        {\n            switch(button)\n            {\n                case MouseButton.Left:\n                case MouseButton.Middle:\n                case MouseButton.Right:\n                case MouseButton.XButton1:\n                case MouseButton.XButton2:\n                    break;\n                default:\n                    throw new  System.ComponentModel.InvalidEnumArgumentException(\"button\", (int)button, typeof(MouseButton));\n            }\n        }\n}\n}\n\n","sourceCodeStart":57,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseButton.cs#L57-L81","documentation":"MouseButton.Validate throws System.ComponentModel.InvalidEnumArgumentException when the button argument is not a defined MouseButton value (Left, Right, Middle, XButton1, XButton2). WPF validates public enum parameters to catch corrupted or miscomputed values early.","triggerScenarios":"Passing an int cast to MouseButton that is outside 0-4, e.g. from P/Invoke data, deserialized state, or bit-flag arithmetic on button values.","commonSituations":"Interoperating with Win32 mouse messages where button codes differ, or computing a button from config/serialization that holds an out-of-range integer.","solutions":["Validate the value against Enum.IsDefined(typeof(MouseButton), value) before passing it","Map external button codes to MouseButton explicitly with a switch","Fix the source that produces the out-of-range value"],"exampleFix":"// before\nMouseButton b = (MouseButton)rawValue;\nMouseButton.Validate(b); // throws\n// after\nif (!Enum.IsDefined(typeof(MouseButton), rawValue))\n{\n    rawValue = (int)MouseButton.Left; // or map explicitly\n}\nMouseButton b = (MouseButton)rawValue;","handlingStrategy":"validation","validationCode":"static bool IsValidMouseButton(MouseButton b) =>\n    b == MouseButton.Left || b == MouseButton.Right || b == MouseButton.Middle ||\n    b == MouseButton.XButton1 || b == MouseButton.XButton2;","typeGuard":"static bool TryParseMouseButton(int raw, out MouseButton button)\n{ if (raw >= 0 && raw <= 4) { button = (MouseButton)raw; return true; } button = MouseButton.Left; return false; }","tryCatchPattern":"try { MouseButton.Validate(button); }\ncatch (InvalidEnumArgumentException) { button = MouseButton.Left; }","preventionTips":["Map Win32/external button codes explicitly instead of casting ints","Use Enum.IsDefined before any enum cast","Never combine MouseButton values with bitwise operators"],"tags":["wpf","mouse","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"}