-3

I have got a ListView box consisting 5 columns with the last column storing a path to an image. My desire is to access the image path and send it to the listView1_MouseClick Event Handler. I have tried a few options, but none have produced the desired result due to my limited knowledge around event handlers. Much appreciated if someone could help me with my quest and THANKS in advance!

below are the two procedures:-

public string GetImageDetailsHandler(object sender, EventArgs e){
      return(listView1.SelectedItems[0].SubItems[4].ToString());

}

private void listView1_MouseClick(object sender, MouseEventArgs e){
     ImageForm image = new ImageForm();

     MouseClick += (sender, e) => { GetImageDetailsHandler(sender, e); };
     image.pictureBox1.Image = Image.FromFile(GetImageDetailsHandler);

    image.ShowDialog();
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
BOB.G
  • 73
  • 9
  • I cannot see what you are hoping to achieve here? I don't even see why you your `GetImageDetailsHandler` should have the parameters you want to pass on to them, as you are not using them, and your `MouseClick` construct is well useless as it is not passing it anywhere it's an anonymous delegate doing that will simply trigger more and more often the longer you click on your listview, what are you trying to do? – Icepickle Aug 17 '17 at 11:14

1 Answers1

1

the event MouseClick will be raised when you click on the listView. Then the method listView1_MouseClick will be executed. What you need is simply just to pull out the path inside this method:

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
     ImageForm image = new ImageForm();

     string path = listView1.SelectedItems[0].SubItems[4].ToString()
     image.pictureBox1.Image = Image.FromFile(path);

     image.ShowDialog();
}

Detailed problem description:

1) when you register the MouseClick event in this line:

MouseClick += (sender, e) => { GetImageDetailsHandler(sender, e); };

you actually register the MouseClick event of the entire Form! so GetImageDetailsHandler will be called everytime you click somewhere on your GUI.

2) although you have a return value in string GetImageDetailsHandler(..) you don't catch is anywhere. So your value is lost. If you really want to use a return value of an event have a look at this post. But to achieve this, you need to fire the event yourself! and this is not the case in your case. The events are raised by the controls!

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Hi Mong... thanks for your help and advice, much appreciated. My thought process led me to believe that I did indeed need to pass the path to the listView1_MouseClick handler so first of all, I went and created something looking like this :- private void listView1_MouseClick(object sender, MouseEventArgs e, string path). Obviously that didn't work, so I went into overdrive searching for a solution on the Internet and came up with the rubbish I presented. Never knew it was that easy! THANKS AGAIN... – BOB.G Aug 18 '17 at 09:58
  • @BOB.G you are welcome. Grasping the event acrogbatics is not the easiest of tasks :) actually with your code you would register on each click on the listview a new MouseClick event of the form. Each one is added! so after clicking 3 times on the listview 3 MouseClicks events are registered and on clicking once anywhere on the form will result in a 3 fold call of the method `GetImageDetailsHandler` ;) Have a nice day – Mong Zhu Aug 18 '17 at 10:40
  • 1
    @ Mong...once again thanks! hopefully one day I will get my head around the acrobatics of Events and their Handlers. – BOB.G Aug 18 '17 at 21:24