简体   繁体   中英

how to get an indication if a control was added to my UserControl ControlsCollection?

I have a user control that acts like a panel in compact framework, and I need to implement AutoSizeMode on it. I've written the code needed to calculate the size of all the controls inside, and everything is working fine.

My only problem is that I don't have any indication on when a control is added or removed from my UserControl.

Currntly, I've added a method to my UserControl to add controls and resize the user control if needed, but this forces anyone that uses this UserControl to go through this method instead of the standard Controls.Add .

Also, I don't know how to get an indication when an inner controls is resized (though that's not going to happen in the foreseeable future, so not so important). (thanks to tcarvin for pointing me to the simple solution of listening to the inner controls resize event)

Here is my current code:

protected void AutoGrow()
{
    if (this.AutoGrowMode != AutoGrowMode.None)
    {
        Size ContentSize = CalculateContentSize();
        int newWidth = this.Size.Width,
            newHeight = this.Size.Height;

        if ((this.AutoGrowMode & AutoGrowMode.Width) == AutoGrowMode.Width && this.Size.Width < ContentSize.Width)
        {
            newWidth = Math.Max(this.Size.Width, ContentSize.Width) + _margin * 2;
        }
        if ((this.AutoGrowMode & AutoGrowMode.Height) == AutoGrowMode.Height && this.Size.Height < ContentSize.Height)
        {
            newHeight = Math.Max(this.Size.Height, ContentSize.Height) + _margin * 2;
        }
        this.Size = new Size(newWidth, newHeight);
        this.Invalidate();
    }
}

protected Size CalculateContentSize()
{
    int MaxBottom = 0,
        MaxRight = 0;
    foreach (Control c in this.Controls)
    {
        MaxBottom = (MaxBottom < c.Bottom) ? c.Bottom : MaxBottom;
        MaxRight = (MaxRight < c.Right) ? c.Right : MaxRight;
    }
    return new Size(MaxRight, MaxBottom);
}

public void AddControl(Control value)
{
    this.Controls.Add(value);
    value.Resize += new EventHandler(ChildControl_Resize);
    AutoGrow();
}

private void ChildControl_Resize(object sender, EventArgs e)
{
    AutoGrow();
}

You could provide your own Controls collection, by adding this to your UserControl code:

    protected override ControlCollection CreateControlsInstance()
    {
        ObservableControlCollection controls = new ObservableControlCollection(this);
        controls.ControlAdded += new Action<Control>(controls_ControlAdded);
        return controls;
    }

    void controls_ControlAdded(Control addedControl)
    {
        Debug.WriteLine("Control added:" + addedControl.Name);
    }

    private sealed class ObservableControlCollection : ControlCollection
    {
        public event Action<Control> ControlAdded;

        public ObservableControlCollection(Control owner)
            : base(owner)
        {
        }

        public override void Add(Control control)
        {
            base.Add(control);

            Action<Control> handler = ControlAdded;
            if (handler != null)
            {
                handler(control);
            }
        }

        // Similarly for removing controls:
        public override void Remove(Control value) { ... }
        public override void Clear() { ... }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM