Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading