65.9K
CodeProject is changing. Read more.
Home

Advanced ListBox

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.19/5 (9 votes)

Dec 29, 2006

viewsIcon

45395

downloadIcon

1125

Advanced ListBox with Header and Can Move Any where that you Like

Sample Image - ListBox0.jpg

Introduction

Some Times you may need a list box with Header and able to Move in Runtime by User Click!
In this Article we design a List Box with Header , Disposable and movable , ...

Background

For design a ListBox with header at first we need a PANEL then we Add a CheckedListBox in it
    public class tblListBox : Panel
    {
        private System.Windows.Forms.CheckedListBox listBox1;
        // Class Detail...
    }

For Movable , Disposable , Header Text, Header Color feature we need some private variable

        bool     isDrag = false;            // For Movable feature
        long     _tblID;                // for table ID
        string     _hText;                // For Header text
        Color     _HeaderColor = Color.Blue;    // For Header Back Color
        bool     _Disposable = false;        // For Disposable feature
        bool     _Movable = false;        // For Movable feature

Constructor:

        public tblListBox(long TableID)
        {
            InitializeComponent();
            _tblID = TableID;
            listBox1.Size = new Size(this.Size.Width, this.Size.Height - 30);
            listBox1.Top = 30;
            this.Controls.Add(listBox1);
            this.MouseMove += new MouseEventHandler(ListMouseMove);
            this.MouseDown += new MouseEventHandler(ListMouseDown);
            this.MouseUp += new MouseEventHandler(ListMouseUp);
            this.DoubleClick += new EventHandler(tblListBox_DoubleClick);
        }

Implementing Class:


        public int SelectedIndex
        {
            get { return listBox1.SelectedIndex; }
        }
        public bool Disposable
        {
            get { return _Disposable; }
            set { _Disposable = value; }
        }
        public bool Movable
        {
            get { return _Movable; }
            set { _Movable = value; }
        }
        
        public string HeaderText
        {
            get { return _hText; }
            set { _hText = value; }
        }
        public long TableID
        {
            get { return _tblID; }
        }
        public Color HeaderColor
        {
            get { return _HeaderColor; }
            set { _HeaderColor = value; }
        }

Now Implementing two property of ListBox: Items, GetItemChecked(int)

        public bool GetItemChecked(int index)
        {
            return listBox1.GetItemChecked(index);
        }
        
        public System.Windows.Forms.ListBox.ObjectCollection Items
        {
            get { return listBox1.Items; }
        }

Now We Need when Panel Resized The ListBox Resized too and Paint New Feature!:

        protected override void OnResize(EventArgs eventargs)
        {
            listBox1.Size = new Size(this.Size.Width, this.Size.Height - 30);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            
            Brush b=new SolidBrush(_HeaderColor);
            // Paint orginall
            base.OnPaint(e);
            // Draw background and fill with _HeaderColor
            e.Graphics.FillRectangle(b, e.ClipRectangle);
            // Determine size of string
            SizeF strSize=e.Graphics.MeasureString(_hText,new Font("Tahoma",8,FontStyle.Bold));
            // Align Center
            float left = e.ClipRectangle.Width/2 - strSize.Width / 2;
            // Draw string on Center of Header
            e.Graphics.DrawString(_hText, new Font("Tahoma", 8, FontStyle.Bold), System.Drawing.Brushes.Yellow, new PointF(left, 10));
        }

Implementing Movable Feature:

        void ListMouseMove(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            tblListBox list = (tblListBox)sender;
            if (isDrag)
            {              
                Point endPoint = this.PointToScreen(new Point(e.X - this.Parent.Location.X-100, e.Y - this.Parent.Location.Y-100));
                list.Location = endPoint;
            }
        }
        void ListMouseUp(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            isDrag = false;
        }
        void ListMouseDown(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            if (e.Button == MouseButtons.Left)
            {
                isDrag = true;
            }
            Control control = (Control)sender;
        }        

Properties:

Properties in Visual Studio 2005