{"record":{"id":"ccf82c8b88e922b9","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"could-not-find-the-parent-form","errorCode":null,"errorMessage":"Could not find the parent form","messagePattern":"Could not find the parent form","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Subsystems/GlobalHotkeys.cs","lineNumber":116,"sourceCode":"            return _registeredHotkeys.GetEnumerator();\n        }\n\n        public bool Remove(HotkeyEntry item)\n        {\n            if (item == null)\n                return false;\n            item.Dispose();\n            return _registeredHotkeys.Remove(item);\n        }\n\n        protected override void OnContainerInitialized(object obj, EventArgs args)\n        {\n            if (ContainerControl is Form form)\n                ParentForm = form;\n            else if (ContainerControl.ParentForm != null)\n                ParentForm = ContainerControl.ParentForm;\n            else\n                throw new InvalidOperationException(\"Could not find the parent form\");\n        }\n\n        protected override void Dispose(bool disposing)\n        {\n            if (disposing)\n            {\n                ParentForm = null;\n                Clear();\n            }\n            base.Dispose(disposing);\n        }\n\n        private void KeyDown_Handler(object sender, KeyEventArgs e)\n        {\n            if (_parentForm == null)\n                throw new InvalidOperationException(\"Hotkey handler called when parent form was null\");\n\n            foreach (var hotkey in _registeredHotkeys)","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Subsystems/GlobalHotkeys.cs#L98-L134","documentation":"Thrown from GlobalHotkeys.OnContainerInitialized. The component must attach its key handling to a Form: it first tries ContainerControl as a Form, then ContainerControl.ParentForm; if neither yields a Form it throws InvalidOperationException because there is nothing to set KeyPreview on. This fires during the designer/host component-initialisation lifecycle, not from user code.","triggerScenarios":"Dropping a GlobalHotkeys component onto a non-Form container (e.g. a UserControl or a component tray whose ContainerControl is not a Form and has no ParentForm), or initialising the component before it has been sited on a form.","commonSituations":"Using GlobalHotkeys inside a UserControl that is not yet parented to a Form at init time, hosting the component in a designer tray without a form context, or calling initialisation manually during construction.","solutions":["Ensure the GlobalHotkeys component is sited on a Form (or a control whose ParentForm resolves) before OnContainerInitialized runs.","Set ParentForm explicitly in code after the form is created: hotkeys.ParentForm = this;","Initialise the component in the Form.OnLoad rather than at construction time."],"exampleFix":"// before: component sited on a UserControl with no parent form yet\n\n// after: assign the parent form explicitly once the form exists\nprotected override void OnLoad(EventArgs e)\n{\n    globalHotkeys1.ParentForm = this;\n    base.OnLoad(e);\n}","handlingStrategy":"validation","validationCode":"// Assign the parent form explicitly before the component initialises\nif (globalHotkeys1.ParentForm == null && this is Form f)\n    globalHotkeys1.ParentForm = f;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Site GlobalHotkeys only on a Form, or set ParentForm in OnLoad.","Avoid parenting the component inside a detached UserControl.","Add a design-time check that ContainerControl resolves to a Form."],"tags":["winforms","lifecycle","components","csharp","kloctools"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}