I want to detect an item by double-clicking in the winforms listbox control. [how to handle a click on an empty area?]

Ok, I have a list with some elements inside.
I want to detect a double click on an element.
currently, the method I'm using has a problem: if the user double-clicks on an empty spot, the currently selected item is signaled by double-clicking.

Update:
Please note that this question is not as simple as it seems at first glance. also note that Timwi's answer is incorrect, because the [if (ListBox1.SelectedIndex == -1)] part is not executed if the item is selected, and I clicked into the empty space I don’t know who supported it, but its answer is incorrect.
I already had this part of the code written
if there is a function that can convert the coordinates of the mouse into a list item, the problem will be fixed.

+3
source share
2 answers

: MouseDoubleClick, MouseEventArgs, . GetItemBounds(), , , , :

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if(listBox1.SelectedIndex != -1)
        {
            var rect = listBox1.GetItemRectangle(listBox1.SelectedIndex);
            if(rect.Contains(e.Location))
            {
                // process item data here
            }
        }
    }

MouseDoubleClick :

  • .NET Framework - : 4, 3.5, 3.0, 2.0
  • .NET Framework - : 4, 3.5 SP1
+14

, MouseClick, .

CheckListBox.CheckOnClick true:   clb.CheckOnClick = true;

:

void clb_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (clb.Items.Count > 0) { clb.SetSelected(0, false); }
    }
+1

Source: https://habr.com/ru/post/1761032/


All Articles