BCUninstaller/Bulk-Crap-Uninstaller · error · InvalidOperationException

ContainerControl can be set only once

Error message

ContainerControl can be set only once

What it means

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.

Source

Thrown at source/KlocTools/Forms/Tools/ReferencedComponent.cs:26

using System.ComponentModel.Design;
using System.Windows.Forms;

namespace Klocman.Forms.Tools
{
    public abstract class ReferencedComponent : Component 
    {
        private ContainerControl _containerControl;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ContainerControl ContainerControl
        {
            get { return _containerControl; }
            set
            {
                if (!DesignMode)
                {
                    if (_containerControl != null)
                        throw new InvalidOperationException("ContainerControl can be set only once");

                    _containerControl = value;

                    if (_containerControl.Visible)
                        ContainerVisibleChanged(this, EventArgs.Empty);
                    else
                        _containerControl.VisibleChanged += ContainerVisibleChanged;
                }
                else
                {
                    _containerControl = value;
                }

                OnContainerControlChanged(this, EventArgs.Empty);
            }
        }

        /// <summary>

View on GitHub (pinned to 608321de98)

Solutions

  1. Create a new component instance for each form/container.
  2. Remove the duplicate runtime assignment.
  3. Only assign ContainerControl during design or once at construction.

Example fix

// before
comp.ContainerControl = formA;
comp.ContainerControl = formB; // throws
// after
var compB = new MyComponent();
compB.ContainerControl = formB;
Defensive patterns

Strategy: validation

Validate before calling

if (component.ContainerControl != null)
    /* create a new component instead of rebinding */

Prevention

When it happens

Trigger: 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.

Common situations: Copy/paste of a component between forms; manually setting ContainerControl after the designer already populated it; sharing a component instance.

Related errors


AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13). Data as JSON: /api/errors/8b2c3fdbb7debbd1. Report an issue: GitHub.