简体   繁体   English

如何获得是否将控件添加到我的UserControl ControlsCollection的指示?

[英]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. 我有一个用户控件,它像紧凑框架中的面板一样工作,并且需要在其上实现AutoSizeMode 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. 我唯一的问题是我没有任何关于何时从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 . 目前,我已经在UserControl中添加了一个方法,可以添加控件并根据需要调整用户控件的大小,但这会迫使使用此UserControl的任何人都使用此方法,而不是标准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) (感谢tcarvin向我指出了监听内部控件resize事件的简单解决方案)

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: 您可以通过将其添加到UserControl代码中来提供自己的Controls集合:

    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() { ... }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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