Custom ListBox

Just a custom ListBox to hold any Control, I made it in C# to hold my UserControls :

 
using System.Windows.Forms;

public class GList : FlowLayoutPanel
{
    public GList()
    {
        this.AutoScroll = true;
        this.AutoSize = false;
        this.FlowDirection = FlowDirection.TopDown;
        this.Margin = this.Padding = new Padding(0, 0, 0, 0);
        this.WrapContents = false;
        this.BorderStyle = BorderStyle.FixedSingle;
    }

    public void Add(Control mCtrl)
    {
        base.Controls.Add(mCtrl);
    }

    public void Remove(string Name)
    {
        var c = base.Controls[Name];
        base.Controls.Remove(c);
        c.Dispose();
    }

    public void RemoveAt(int Index)
    {
        var c = base.Controls[Index];
        base.Controls.Remove(c);
        c.Dispose();
    }

    public void Clear()
    {
        while (base.Controls.Count > 0)
        {
            var c = base.Controls[0];
            base.Controls.Remove(c);
            c.Dispose();
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        SetupAnchors();
        base.OnPaint(e);
    }

    void SetupAnchors()
    {
        if (base.Controls.Count > 0)
        {
            for (int i = 0; i < base.Controls.Count; i++)
            {
                var c = base.Controls[i];
                if (i == 0)
                {
                    c.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                    c.Width = this.Width - SystemInformation.VerticalScrollBarWidth - 10;
                }
                else
                    c.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            }
        }
    }
}



Source : https://www.codeproject.com/Articles/333864/Flexible-List-Control


No comments:

Post a Comment