Why does c# winforms ListView.Click event handler only work when you click on the text contained in the list view and not in the whole of the control?

Advertisements

Here is my code:

ListView listview = new ListView();
listview.Size = new System.Drawing.Size(150, 100);
listview.BackColor = System.Drawing.Color.Orange;
listview.ForeColor = System.Drawing.Color.Black;
listview.Name = "Group" + count2;
listview.FullRowSelect = true; // just tried this
listview.Click += HighLight; 
// no Position is set because its added to a FlowLayoutPanel
foreach (var item in text_list)
{
    listview.Items.Add(item); // adds the text
}
autolayoutGroups.Controls.Add(listview); // add to FlowlayoutPanel

As you can see I set a function to be called when the user clicks on the listview but it only works when you click on the text in the listview. How can I make it call the function when you click anywhere within the control?

>Solution :

Use the event mousedown to detect clicks in the entire listview

add mousedown event

listView1.MouseDown += ListView1_MouseDown;

implementation

 private void ListView1_MouseDown(object sender, MouseEventArgs e)
 {
    throw new NotImplementedException();
 }

Leave a ReplyCancel reply