views:

434

answers:

4

I am trying to do something when double clicking an item in a ListBox. I have found this code for doing that

void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        int index = this.listBox1.IndexFromPoint(e.Location);
        if (index != System.Windows.Forms.ListBox.NoMatches)
        {
            MessageBox.Show(index.ToString());
            //do your stuff here
        }
    }

However, when i click on an item, the event isn't fired. The event is fired if i click in the ListBox below all the items.

I set the DataSource property of the ListBox to IList<MyObject>.

Any ideas?

+1  A: 

Works for me, so I assume there might be something about the items in the list (custom? intercepting the event?) or the event is not properly wired up.

For example try this (complete Form1.cs):

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
   public class MyObject {
      public MyObject(string someValue) {
         SomeValue = someValue;
      }

      protected string SomeValue { get; set; }

      public override string ToString() {
         return SomeValue;
      }
   }


   public partial class Form1 : Form {
      public Form1() {
         InitializeComponent();

         var list = new List<MyObject> { 
            new MyObject("Item one"), new MyObject("Item two")
         };
         listBox1.DataSource = list;

      }

      private void listBox1_DoubleClick(object sender, EventArgs e) {
         Debug.WriteLine("DoubleClick event fired on ListBox");
      }
   }
}

With the designer source file (Form1.Designer.cs) containing this:

private void InitializeComponent() {
   this.listBox1 = new System.Windows.Forms.ListBox();
   ... // left out for brevity
   this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);

As a test, create a new Forms base application through the templates, then add just the ListBox and define a class MyObject. See whether you observe the same or a different behavior.

John
A: 

John: then it works. But i figured out that the event isn't fired because I am also handling the MouseDown event. I tried to remove the MouseDown handling, and then it works. Is there a smooth way to handle both those events? If not, I just have to find some other way to catch a double click through the MouseDown event.

Hans Espen
+2  A: 

Tried creating a form with a ListBox with MouseDown and DoubleClick events. As far as I can see, the only situation, when DoubleClick won't fire, is if inside the MouseDown you call the MessageBox.Show(...). In other cases it works fine.

And one more thing, I don't know for sure, if it is important, but anyway. Of course, you can get the index of the item like this:

int index = this.listBox1.IndexFromPoint(e.Location);

But this way is fine as well:

if (listBox1.SelectedItem != null)
    ...
26071986
This is what i do inside my MouseDown:listBox.DoDragDrop(item, DragDropEffects.Copy | DragDropEffects.Move);
Hans Espen
@Hans Espen I see, it's blocking DoubleClick as well...So, you can do it this way: inside the MouseDown check the number of clicks (e.Clicks). If the e.Clicks is equal to 1, then call DoDragDrop, otherwise perform all the actions for the DoubleClick.
26071986
A: 

Thank you for all replies. It now works. I solved it, like 26071986 said, with handling double click in the MouseDown handler by checking if e.Clicks is 1. If so, I call DoDragDrop, if not, I call the method that handles double click.

private void MouseDown_handler(object sender, MouseEventArgs e)
    {
        var listBox = (ListBox) sender;

        if (e.Clicks != 1)
        {
            DoubleClick_handler(listBox1.SelectedItem);
            return;
        }

        var pt = new Point(e.X, e.Y);
        int index = listBox.IndexFromPoint(pt);

        // Starts a drag-and-drop operation with that item.
        if (index >= 0)
        {
            var item = (listBox.Items[index] as MyObject).CommaDelimitedString();
            listBox.DoDragDrop(item, DragDropEffects.Copy | DragDropEffects.Move);
        }
    }
Hans Espen