5

双击 ListBox 中的项目时,我正在尝试做某事。我找到了这样做的代码

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
        }
    }

但是,当我单击一个项目时,不会触发该事件。如果我单击所有项目下方的列表框,则会触发该事件。

我将 DataSource 的属性设置ListBoxIList<MyObject>

有任何想法吗?

4

5 回答 5

5

尝试使用带有 MouseDown 和 DoubleClick 事件的 ListBox 创建表单。据我所知,DoubleClick 不会触发的唯一情况是,如果在 MouseDown 内调用 MessageBox.Show(...)。在其他情况下,它工作正常。

还有一件事,我不确定它是否重要,但无论如何。当然,您可以像这样获取项目的索引:

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

但这种方式也很好:

if (listBox1.SelectedItem != null)
    ...
于 2010-08-09T06:28:30.000 回答
1

这是我在 MouseDoubleClick 事件中使用的内容。

    private void YourMethodForDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Type sourceType = e.OriginalSource.GetType();
        if (sourceType != typeof(System.Windows.Controls.TextBlock)
            && sourceType != typeof(System.Windows.Controls.Border))
            return;

        //if you get here, it's one of the list items.

        DoStuff();
        ...
    }
于 2011-03-21T18:16:11.130 回答
1

谢谢大家的回复。现在可以了。我解决了它,就像 26071986 说的那样,通过检查 e.Clicks 是否为 1 在 MouseDown 处理程序中处理双击。如果是,我调用 DoDragDrop,如果不是,我调用处理双击的方法。

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);
        }
    }
于 2010-08-09T10:03:05.477 回答
1

对我有用,所以我假设列表中的项目可能存在某些问题(自定义?拦截事件?)或者事件没有正确连接。

例如试试这个(完整的 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");
      }
   }
}

使用包含以下内容的设计器源文件 (Form1.Designer.cs):

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

作为测试,通过模板创建一个新的 Forms 基础应用程序,然后只添加 ListBox 并定义一个 MyObject 类。查看您是否观察到相同或不同的行为。

于 2010-08-08T12:24:30.037 回答
0

约翰:那么它起作用了。但我发现该事件没有被触发,因为我也在处理 MouseDown 事件。我试图删除 MouseDown 处理,然后它工作。有没有一种流畅的方法来处理这两个事件?如果没有,我只需要找到其他方法来通过 MouseDown 事件捕获双击。

于 2010-08-09T06:11:21.593 回答