BCUninstaller/Bulk-Crap-Uninstaller · error · InvalidOperationException
Could not find the parent form
Error message
Could not find the parent form
What it means
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.
Source
Thrown at source/KlocTools/Subsystems/GlobalHotkeys.cs:116
return _registeredHotkeys.GetEnumerator();
}
public bool Remove(HotkeyEntry item)
{
if (item == null)
return false;
item.Dispose();
return _registeredHotkeys.Remove(item);
}
protected override void OnContainerInitialized(object obj, EventArgs args)
{
if (ContainerControl is Form form)
ParentForm = form;
else if (ContainerControl.ParentForm != null)
ParentForm = ContainerControl.ParentForm;
else
throw new InvalidOperationException("Could not find the parent form");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ParentForm = null;
Clear();
}
base.Dispose(disposing);
}
private void KeyDown_Handler(object sender, KeyEventArgs e)
{
if (_parentForm == null)
throw new InvalidOperationException("Hotkey handler called when parent form was null");
foreach (var hotkey in _registeredHotkeys)View on GitHub (pinned to 608321de98)
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.
Example fix
// before: component sited on a UserControl with no parent form yet
// after: assign the parent form explicitly once the form exists
protected override void OnLoad(EventArgs e)
{
globalHotkeys1.ParentForm = this;
base.OnLoad(e);
} Defensive patterns
Strategy: validation
Validate before calling
// Assign the parent form explicitly before the component initialises
if (globalHotkeys1.ParentForm == null && this is Form f)
globalHotkeys1.ParentForm = f; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Hotkey handler called when parent form was null
- Minimum value must be lower than or equal to the maximum val
- Selector is invalid, it has to be in format x => x.Property
- Resource of this type doesn't contain specified property
- Specified property is not of string type
AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13).
Data as JSON: /api/errors/ccf82c8b88e922b9.
Report an issue: GitHub.