{"record":{"id":"9fd116930e0c02ee","repo":"dotnet/wpf","slug":"sr-automation-invalideventid","errorCode":null,"errorMessage":"SR.Automation_InvalidEventId","messagePattern":"SR\\.Automation_InvalidEventId","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/EventMap.cs","lineNumber":110,"sourceCode":"                case AutomationEvents.InvokePatternOnInvoked:                               eventObject = InvokePatternIdentifiers.InvokedEvent; break;\n                case AutomationEvents.SelectionItemPatternOnElementAddedToSelection:        eventObject = SelectionItemPatternIdentifiers.ElementAddedToSelectionEvent; break;\n                case AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection:    eventObject = SelectionItemPatternIdentifiers.ElementRemovedFromSelectionEvent; break;\n                case AutomationEvents.SelectionItemPatternOnElementSelected:                eventObject = SelectionItemPatternIdentifiers.ElementSelectedEvent; break;\n                case AutomationEvents.SelectionPatternOnInvalidated:                        eventObject = SelectionPatternIdentifiers.InvalidatedEvent; break;\n                case AutomationEvents.TextPatternOnTextSelectionChanged:                    eventObject = TextPatternIdentifiers.TextSelectionChangedEvent; break;\n                case AutomationEvents.TextPatternOnTextChanged:                             eventObject = TextPatternIdentifiers.TextChangedEvent; break;\n                case AutomationEvents.AsyncContentLoaded:                                   eventObject = AutomationElementIdentifiers.AsyncContentLoadedEvent; break;\n                case AutomationEvents.PropertyChanged:                                      eventObject = AutomationElementIdentifiers.AutomationPropertyChangedEvent; break;\n                case AutomationEvents.StructureChanged:                                     eventObject = AutomationElementIdentifiers.StructureChangedEvent; break;\n                case AutomationEvents.InputReachedTarget:                                   eventObject = SynchronizedInputPatternIdentifiers.InputReachedTargetEvent; break;\n                case AutomationEvents.InputReachedOtherElement:                             eventObject = SynchronizedInputPatternIdentifiers.InputReachedOtherElementEvent; break;\n                case AutomationEvents.InputDiscarded:                                       eventObject = SynchronizedInputPatternIdentifiers.InputDiscardedEvent; break;\n                case AutomationEvents.LiveRegionChanged:                                    eventObject = AutomationElementIdentifiers.LiveRegionChangedEvent; break;\n                case AutomationEvents.Notification:                                         eventObject = AutomationElementIdentifiers.NotificationEvent; break;\n                case AutomationEvents.ActiveTextPositionChanged:                            eventObject = AutomationElementIdentifiers.ActiveTextPositionChangedEvent; break;\n\n                default:\n                    throw new ArgumentException(SR.Automation_InvalidEventId, nameof(eventId));\n            }\n\n            if ((eventObject != null) && (!_eventsTable.ContainsKey(eventObject.Id)))\n            {\n                eventObject = null;\n            }\n\n            return (eventObject);\n        }\n\n        internal static void AddEvent(int idEvent)\n        {\n            //  to avoid unbound memory allocations,\n            //  register only events that we recognize\n            if (IsKnownEvent(idEvent))\n            {\n                bool firstEvent = false;\n                lock (_lock)","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/EventMap.cs#L92-L128","documentation":"EventMap.GetRegisteredEventObjectHelper maps an AutomationEvents enum value to the corresponding UIA event identifier. If the eventId is not one of the known enum cases, the method throws ArgumentException with SR.Automation_InvalidEventId. This guards the internal event registration table against unknown event ids.","triggerScenarios":"Calling RaiseAutomationEvent / HasRegisteredEvent / GetRegisteredEvent with an AutomationEvents value outside the defined enum range — e.g. an invalid cast, a value from a newer/older framework version, or uninitialized/default misuse.","commonSituations":"Custom control authors raising automation events with a hand-crafted or wrongly cast enum value; assembly/version mismatch where an AutomationEvents member exists on one side but not the mapped table; reflection-driven code passing raw ints as the enum.","solutions":["Use only valid AutomationEvents members (e.g. AutomationEvents.AutomationFocusChanged); never cast raw ints into the enum.","Check the enum value with Enum.IsDefined(typeof(AutomationEvents), value) before raising.","Rebuild against the matching WindowsBase/PresentationCore versions so the enum and event map agree.","In custom peer code, switch on the enum and let unknown values fall through without throwing."],"exampleFix":"// before\npeer.RaiseAutomationEvent((AutomationEvents)99);\n// after\nvar eventId = AutomationEvents.LiveRegionChanged;\nif (Enum.IsDefined(typeof(AutomationEvents), eventId))\n{\n    peer.RaiseAutomationEvent(eventId);\n}","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(AutomationEvents), eventId))\n    throw new ArgumentOutOfRangeException(nameof(eventId));","typeGuard":"bool IsValidAutomationEvent(AutomationEvents e) => Enum.IsDefined(typeof(AutomationEvents), e);","tryCatchPattern":"try\n{\n    peer.RaiseAutomationEvent(eventId);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"eventId\")\n{\n    // log invalid event id and skip raise\n}","preventionTips":["Only use named AutomationEvents members, never raw int casts","Validate enum values with Enum.IsDefined before raising events","Keep PresentationCore/WindowsBase reference versions aligned","In custom peers, switch on known cases and ignore unknown ones"],"tags":["wpf","ui-automation","argumentexception","invalid-enum"],"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"}