I've used this in the past, and modified it to handle left-click-Alt: it let's you select/de-select all with either alt-down and left click, or control-A/a: if all are selected it de-selects all.
To build this: Add a new Project Item to your WinForm Project of Type Component named : ListBoxWithSelectAll, and paste in the following code:
using System;
using System.Windows.Forms;
namespace CustomControls
{
public partial class ListBoxWithSelectAll : ListBox
{
public ListBoxWithSelectAll()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
private bool MouseIsDown = false;
private bool AllLBItemsSelected = false;
private void ListBoxWithSelectAll_KeyDown(object sender, KeyEventArgs e)
{
if ((MouseIsDown && e.Alt && MouseButtons == MouseButtons.Left) || (e.Control && e.KeyCode == Keys.A))
{
AllLBItemsSelected = !AllLBItemsSelected;
for (int i = 0; i < this.Items.Count; i++)
{
this.SetSelected(i, AllLBItemsSelected);
}
MouseIsDown = false;
}
}
private void ListBoxWithSelectAll_MouseDown(object sender, MouseEventArgs e)
{
MouseIsDown = true;
}
private void ListBoxWithSelectAll_MouseUp(object sender, MouseEventArgs e)
{
MouseIsDown = false;
}
private void ListBoxWithSelectAll_Enter(object sender, EventArgs e)
{
if (this.SelectionMode == SelectionMode.One || this.SelectionMode == SelectionMode.None)
{
this.SelectionMode = SelectionMode.MultiSimple;
}
}
}
}
What I changed: this was adapted from some code that also implemented an 'Undo mechanism for selections, but I can't share that since it "belongs" to a client.
This code has been tested in FrameWork 4.5 and 3.0. It works with the custom sub-classed ListBox 'SelectionMode set to either 'MultiSimple or 'MultiExtended modes.