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>
}
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>
<FONT color=#0000ff>long</FONT> _tblID; <FONT color=#008000>
<FONT color=#0000ff>string</FONT> _hText; <FONT color=#008000>
<FONT color=#0000ff>Color</FONT> _HeaderColor = <FONT color=#cc99ff>Color</FONT>.<FONT color=#0000ff>Blue</FONT>; <FONT color=#008000>
<FONT color=#0000ff>bool</FONT> _Disposable =<FONT color=#0000ff> false</FONT>; <FONT color=#008000>
<FONT color=#0000ff>bool</FONT> _Movable = <FONT color=#0000ff>false</FONT>; <FONT color=#008000>
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>
</FONT> <FONT color=#0000ff>base</FONT>.OnPaint(e);
<FONT color=#008000>
</FONT> e.Graphics.FillRectangle(b, e.ClipRectangle);
<FONT color=#008000>
<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>
float left = e.ClipRectangle.Width/2 - strSize.Width / 2;
<FONT color=#008000>
</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: