{"record":{"id":"8b2c3fdbb7debbd1","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"containercontrol-can-be-set-only-once","errorCode":null,"errorMessage":"ContainerControl can be set only once","messagePattern":"ContainerControl can be set only once","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Forms/Tools/ReferencedComponent.cs","lineNumber":26,"sourceCode":"using System.ComponentModel.Design;\nusing System.Windows.Forms;\n\nnamespace Klocman.Forms.Tools\n{\n    public abstract class ReferencedComponent : Component \n    {\n        private ContainerControl _containerControl;\n\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]\n        public ContainerControl ContainerControl\n        {\n            get { return _containerControl; }\n            set\n            {\n                if (!DesignMode)\n                {\n                    if (_containerControl != null)\n                        throw new InvalidOperationException(\"ContainerControl can be set only once\");\n\n                    _containerControl = value;\n\n                    if (_containerControl.Visible)\n                        ContainerVisibleChanged(this, EventArgs.Empty);\n                    else\n                        _containerControl.VisibleChanged += ContainerVisibleChanged;\n                }\n                else\n                {\n                    _containerControl = value;\n                }\n\n                OnContainerControlChanged(this, EventArgs.Empty);\n            }\n        }\n\n        /// <summary>","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Forms/Tools/ReferencedComponent.cs#L8-L44","documentation":"ReferencedComponent binds to a single ContainerControl (its owning form) at runtime and disallows reassignment once set, because it subscribes visibility events and assumes one owner. At design time (DesignMode) reassignment is permitted so the designer can serialize the property. Reassigning at runtime throws InvalidOperationException.","triggerScenarios":"Assigning ContainerControl twice on the same instance at runtime; reusing one component across two forms; the designer serialized the property and code also sets it at runtime.","commonSituations":"Copy/paste of a component between forms; manually setting ContainerControl after the designer already populated it; sharing a component instance.","solutions":["Create a new component instance for each form/container.","Remove the duplicate runtime assignment.","Only assign ContainerControl during design or once at construction."],"exampleFix":"// before\ncomp.ContainerControl = formA;\ncomp.ContainerControl = formB; // throws\n// after\nvar compB = new MyComponent();\ncompB.ContainerControl = formB;","handlingStrategy":"validation","validationCode":"if (component.ContainerControl != null)\n    /* create a new component instead of rebinding */","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use one component instance per container form.","Avoid runtime reassignment of ContainerControl."],"tags":["winforms","designer","component"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}