Click here to Skip to main content
Licence 
First Posted 29 Dec 2006
Views 29,202
Bookmarked 21 times

Advanced ListBox

By | 29 Dec 2006 | Article
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
    <FONT color=#0000ff>public class</FONT> tblListBox : <FONT color=#cc99ff>Panel</FONT>
    {
        <FONT color=#0000ff>private</FONT> <FONT color=#cc99ff>System.Windows.Forms.CheckedListBox</FONT> listBox1;
<FONT color=#009933>        // Class Detail...</FONT>
    }

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

        <FONT color=#0000ff>bool</FONT>     isDrag = <FONT color=#0000ff>false</FONT>;            <FONT color=#008000>// For Movable feature</FONT>
        <FONT color=#0000ff>long</FONT>     _tblID;                <FONT color=#008000>// for table ID</FONT>
        <FONT color=#0000ff>string</FONT>     _hText;                <FONT color=#008000>// For Header text</FONT>
        <FONT color=#0000ff>Color</FONT>     _HeaderColor = <FONT color=#cc99ff>Color</FONT>.<FONT color=#0000ff>Blue</FONT>;    <FONT color=#008000>// For Header Back Color</FONT>
        <FONT color=#0000ff>bool</FONT>     _Disposable =<FONT color=#0000ff> false</FONT>;        <FONT color=#008000>// For Disposable feature</FONT>
        <FONT color=#0000ff>bool</FONT>     _Movable = <FONT color=#0000ff>false</FONT>;        <FONT color=#008000>// For Movable feature</FONT>

Constructor:

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

Implementing Class:


        <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>int</FONT> SelectedIndex
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> listBox1.SelectedIndex; }
        }
        <FONT color=#0000ff>public bool</FONT> Disposable
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> _Disposable; }
            <FONT color=#0000ff>set</FONT> { _Disposable = <FONT color=#0000ff>value</FONT>; }
        }
       <FONT color=#0000ff> public bool</FONT> Movable
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> _Movable; }
            <FONT color=#0000ff>set</FONT> { _Movable = <FONT color=#0000ff>value</FONT>; }
        }
        
       <FONT color=#0000ff> public string</FONT> HeaderText
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> _hText; }
            <FONT color=#0000ff>set</FONT> { _hText = <FONT color=#0000ff>value</FONT>; }
        }
        <FONT color=#0000ff>public long</FONT> TableID
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> _tblID; }
        }
        <FONT color=#0000ff>public Color</FONT> HeaderColor
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> _HeaderColor; }
            <FONT color=#0000ff>set</FONT> { _HeaderColor = <FONT color=#0000ff>value</FONT>; }
        }

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

        <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>bool</FONT> GetItemChecked(<FONT color=#0000ff>int</FONT> index)
        {
            <FONT color=#0000ff>return</FONT> listBox1.GetItemChecked(index);
        }
        
        <FONT color=#0000ff>public</FONT> <FONT color=#008080>System.Windows.Forms.ListBox.ObjectCollection</FONT> Items
        {
            <FONT color=#0000ff>get</FONT> { <FONT color=#0000ff>return</FONT> listBox1.Items; }
        }

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

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

Implementing Movable Feature:

        <FONT color=#0000ff>void</FONT> ListMouseMove(<FONT color=#0000ff>object</FONT> sender, MouseEventArgs e)
        {
            <FONT color=#0000ff>if</FONT> (!_Movable) <FONT color=#0000ff>return</FONT>;
            tblListBox list = (tblListBox)sender;
            <FONT color=#0000ff>if</FONT> (isDrag)
            {              
                Point endPoint = <FONT color=#0000ff>this</FONT>.PointToScreen(<FONT color=#0000ff>new</FONT> Point(e.X - <FONT color=#0000ff>this</FONT>.Parent.Location.X-100, e.Y - <FONT color=#0000ff>this</FONT>.Parent.Location.Y-100));
                list.Location = endPoint;
            }
        }
        <FONT color=#0000ff>void</FONT> ListMouseUp(<FONT color=#0000ff>object</FONT> sender, MouseEventArgs e)
        {
            <FONT color=#0000ff>if</FONT> (!_Movable) <FONT color=#0000ff>return</FONT>;
            isDrag = <FONT color=#0000ff>false</FONT>;
        }
        <FONT color=#0000ff>void</FONT> ListMouseDown(<FONT color=#0000ff>object</FONT> sender, MouseEventArgs e)
        {
            <FONT color=#0000ff>if</FONT> (!_Movable) <FONT color=#0000ff>return</FONT>;
            <FONT color=#0000ff>if</FONT> (e.Button == MouseButtons.Left)
            {
                isDrag = <FONT color=#0000ff>true</FONT>;
            }
            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   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberReymonARG8:31 20 Feb '10  
GeneralMy vote of 2 Pinmembersotona6:43 30 Nov '08  
GeneralThanks PinmemberAmir Mehrabi J.17:28 4 Jan '07  
Thanks for your comment.
 
Amir Mehrabi Jorshary

Generalgreat Pinmemberdino66614:20 29 Dec '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 29 Dec 2006
Article Copyright 2006 by Amir Mehrabi-Jorshary
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid