control that grows

Its a nice graphical display and makes it comfortable to use having a control like a Textbox Listbox or even a Gridview grow when focused or hovered by the mouse, i made a small class that will manage that.

Passing any control to the GrowManager will allow a control to expand on certain events (OnMOuseOver, OnFocus etc…) the size of growth can be determine on the constructor or on the properties exposed by the GrowManager class.

here’s the code:

public class GrowManager
{
    // Methods
    public GrowManager(Control owner)
    {
        this.Owner = owner;
        this.Owner.MouseEnter += new EventHandler(this.Owner_MouseEnter);
        this.Owner.MouseLeave += new EventHandler(this.Owner_MouseLeave);
        this.Owner.Enter += new EventHandler(this.Owner_Enter);
        this.Owner.Leave += new EventHandler(this.Owner_Leave);
    }

    public GrowManager(Control owner, int MaxHeight, int MaxWidth)
    {
        this.Owner = owner;
        this._MaxHeight = MaxHeight;
        this._MaxWidth = MaxWidth;
        this.Owner.MouseEnter += new EventHandler(this.Owner_MouseEnter);
        this.Owner.MouseLeave += new EventHandler(this.Owner_MouseLeave);
        this.Owner.Enter += new EventHandler(this.Owner_Enter);
        this.Owner.Leave += new EventHandler(this.Owner_Leave);
    }

    ~GrowManager()
    {
        this.Owner.MouseEnter -= new EventHandler(this.Owner_MouseEnter);
        this.Owner.MouseLeave -= new EventHandler(this.Owner_MouseLeave);
        this.Owner.Enter -= new EventHandler(this.Owner_Enter);
        this.Owner.Leave -= new EventHandler(this.Owner_Leave);
    }

    private void Grow()
    {
        if (!this.isInGrowthMode)
        {
            int Dif = 0;
            int n = 0;
            this.Owner.SuspendLayout();
            this._OrigSize = this.Owner.Size;
            this._origLocation = this.Owner.Location;
            int growToHeight = this._MaxHeight;
            int growToWidth = this._MaxWidth;
            int LocationX = this.Owner.Location.X;
            int LocationY = this.Owner.Location.Y;
            bool GrowVertical = false;
            bool GrowHorizontal = false;
            if ((growToHeight > this.Owner.Height))
            {
                LocationY = (int)(LocationY - ((growToHeight - this.Owner.Height) / 2));
                Dif = 0;
                if ((LocationY < 0))
                {
                    Dif = Math.Abs(LocationY);
                }
                if ((((this.Owner.Parent != null)) && (this.Owner.Parent.Height < (LocationY + growToHeight))))
                {
                    n = (Math.Abs((int)((LocationY + growToHeight) - this.Owner.Parent.Height)) + 8);
                    Dif = (int)((n > Dif) ? n : Dif);
                }
                if ((Dif > 0))
                {
                    growToHeight = (growToHeight - ((Dif * 2) + 2));
                    LocationY = (int)(this.Owner.Location.Y - ((growToHeight - this.Owner.Height) / 2));
                }
                GrowVertical = true;
            }
            if ((growToWidth > this.Owner.Width))
            {
                LocationX = (int)(LocationX - ((growToWidth - this.Owner.Width) / 2));
                Dif = 0;
                if ((LocationX < 0))
                {
                    Dif = Math.Abs(LocationX);
                }
                if ((((this.Owner.Parent != null)) && (this.Owner.Parent.Width < (LocationX + growToWidth))))
                {
                    n = (Math.Abs((int)((LocationX + growToWidth) - this.Owner.Parent.Width)) + 8);
                    Dif = (int)((n > Dif) ? n : Dif);
                }
                if ((Dif > 0))
                {
                    growToWidth = (growToWidth - ((Dif * 2) + 2));
                    LocationX = (int)(this.Owner.Location.X - ((growToWidth - this.Owner.Width) / 2));
                }
                GrowHorizontal = true;
            }
            if (GrowVertical)
            {
                this.Owner.Height = growToHeight;
            }
            if (GrowHorizontal)
            {
                this.Owner.Width = growToWidth;
            }
            this.Owner.Location = new Point(LocationX, LocationY);
            this.Owner.BringToFront();
            this.isInGrowthMode = true;
            this.Owner.ResumeLayout(false);
        }
    }

    private void GrowBack()
    {
        this.Owner.Location = this._origLocation;
        this.Owner.Size = this._OrigSize;
        this.Owner.SendToBack();
        this.isInGrowthMode = false;
    }

    private void Owner_Enter(object sender, EventArgs e)
    {
        this.Grow();
    }

    private void Owner_Leave(object sender, EventArgs e)
    {
        this.GrowBack();
    }

    private void Owner_MouseEnter(object sender, EventArgs e)
    {
        this.Grow();
        if (_FocusOnMouseEnter) Owner.Focus();
    }

    private void Owner_MouseLeave(object sender, EventArgs e)
    {
        this.GrowBack();
    }

    // Properties
    public bool FocusOnMouseEnter
    {
        get { return _FocusOnMouseEnter; }
        set { _FocusOnMouseEnter = value; }
    }

    public int MaxHeight
    {
        get { return this._MaxHeight; }
        set { this._MaxHeight = value; }
    }

    public int MaxWidth
    {
        get { return this._MaxWidth; }
        set { this._MaxWidth = value; }
    }

    // Fields
    private bool _FocusOnMouseEnter;
    private int _MaxHeight;
    private int _MaxWidth;
    private Point _origLocation;
    private Size _OrigSize;
    private bool isInGrowthMode;
    protected Control Owner;
}
Follow

Get every new post delivered to your Inbox.