Click here to Skip to main content
15,898,947 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi all,
I have a listview. Its items are displayed and click on each item is not a problem. But while that listview is docked (fill) into a panel. When I click on an empty area of a listview (I believe), I means an empty row of that listview in that panel. My current selectedItem will be changed to false. I dont know what event handle that click at an empty area of a listview. Then I can disable it...


Thanks in advance.
Posted
Updated 4-Mar-11 3:40am
v3

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)
            {
                // Ignore mouse messages not in the client area 
                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()
        {
            // Create a ListView
            MyListView listview = new MyListView();
            listview.Dock = DockStyle.Fill;
            listview.View = View.Details;
            listview.CausesValidation = false;
            // ... add some columns
            listview.Columns.Add("Name");
            listview.Columns.Add("Value");
            // ... and dummy items
            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);
            };
           
            // Create a Test-Form
            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).
 
Share this answer
 
C#
listView1_SelectedIndeChaned(object sender, EventArgs e)
   {
   try
   {
        // your code here
   }
   catch
   {
        // leave empty to catch error
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900