Click here to Skip to main content
15,888,461 members
Articles / Programming Languages / C#
Article

Advanced ListBox

Rate me:
Please Sign up or sign in to vote.
3.19/5 (9 votes)
29 Dec 2006 45K   1.1K   21   4
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
C#
    <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

C#
<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:

C#
<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:


C#
 <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)

C#
<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!:

C#
        <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:

C#
<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


Written By
Software Developer (Senior) Omid Farda IT Company
Iran (Islamic Republic of) Iran (Islamic Republic of)
Master of Science in Computer Engineering

Comments and Discussions

 
GeneralMy vote of 2 Pin
ReymonARG20-Feb-10 8:31
ReymonARG20-Feb-10 8:31 
GeneralMy vote of 2 Pin
dethtroll30-Nov-08 6:43
dethtroll30-Nov-08 6:43 
GeneralThanks Pin
Amir Mehrabi-Jorshari4-Jan-07 17:28
Amir Mehrabi-Jorshari4-Jan-07 17:28 
Generalgreat Pin
dinolee29-Dec-06 14:20
dinolee29-Dec-06 14:20 

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

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