Click here to Skip to main content
3.00 / 5, 7 votes

1
4 votes, 57.1%
2
1 vote, 14.3%
3
1 vote, 14.3%
4
1 vote, 14.3%
5

Advanced ListBox

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Amir Mehrabi-Jorshary


Software Developer (Senior)
Omid Farda IT Company
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member
Master of Science in Computer Engineering

Sign Up to vote for this article
Add a reason or comment to your vote:

Comments and Discussions

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberReymonARG9:31 20 Feb '10  
GeneralMy vote of 2 Pinmembersotona7:43 30 Nov '08  
GeneralThanks PinmemberAmir Mehrabi J.18:28 4 Jan '07  
Generalgreat Pinmemberdino66615:20 29 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 29 Dec 2006

Copyright 2006 by Amir Mehrabi-Jorshary
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project