Hi Apurva,
This is one possible solution for this: Ignore the mouse-click in the client area of the ListView.
using System;
using System.Windows.Forms;
using System.Drawing;
namespace ListViewFocusDemo
{
static class Program
{
class MyListView : ListView
{
protected override void WndProc(ref Message msg)
{
if (msg.Msg >= 0x201 && msg.Msg <= 0x209)
{
Point pointMousePos = new Point(msg.LParam.ToInt32() & 0xffff, msg.LParam.ToInt32() >> 16);
ListViewHitTestInfo lvhti = this.HitTest(pointMousePos);
switch (lvhti.Location)
{
case ListViewHitTestLocations.AboveClientArea:
case ListViewHitTestLocations.BelowClientArea:
case ListViewHitTestLocations.LeftOfClientArea:
case ListViewHitTestLocations.RightOfClientArea:
case ListViewHitTestLocations.None:
return;
}
}
base.WndProc(ref msg);
}
}
static void Main()
{
MyListView listview = new MyListView();
listview.Dock = DockStyle.Fill;
listview.View = View.Details;
listview.CausesValidation = false;
listview.Columns.Add("Name");
listview.Columns.Add("Value");
listview.Items.Add(new ListViewItem(new string[] { "IsListView", "Yes" }));
listview.Items.Add(new ListViewItem(new string[] { "IsDoingWhatIWant", "No"}));
Label label = new Label();
label.Dock = DockStyle.Bottom;
listview.SelectedIndexChanged += delegate(object obj, EventArgs ea)
{
label.Text = String.Format("Selected Index = {0}",
listview.SelectedIndices.Count > 0 ? listview.SelectedIndices[0] : -1);
};
Form form = new Form();
form.Controls.Add(listview);
form.Controls.Add(label);
Application.Run(form);
}
}
}
Another solution would be to recreate the selection if the unselection occurs (SelectedIndexChanged, ItemSelectionChanged).