C# Listbox Item Double Click Event

后端 未结 6 753
天命终不由人
天命终不由人 2020-12-08 06:00

I have a list box with some items. Is there anyway I can attach a double click event to each item?

Item 1
Item 2
Item 3

If i was to double

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 06:49

    For Winforms

    private void listBox1_DoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.listBox1.IndexFromPoint(e.Location);
            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                MessageBox.Show(listBox1.SelectedItem.ToString());
            }
        }
    

    and

    public Form()
    {
        InitializeComponent();
        listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_DoubleClick);
    }
    

    that should also, prevent for the event firing if you select an item then click on a blank area.

提交回复
热议问题