C # Choose a double list of clicks

I have problems with some C #. I have a list, when I double-click an entry, I want to return the row that I double-clicked.

How to do it?

+4
source share
1 answer

I assume you are using WinForms.

If you work with one choice, it is quite simple: on the double-click handler (please check how to do this with Google or see later) check the property SelectedItem. A double-click item is also selected.

void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
    var list = (ListBox)sender;

    // This is your selected item
    object item = list.SelectedItem;
}

If you are working with multiple selections, you need to check more which item was clicked because it may be the last selection, you can use the method IndexFromPoint()as follows:

void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
    var list = (ListBox)sender;

    int itemIndex = list.IndexFromPoint(e.Location);
    if (itemIndex != -1)
    {
        // This is your double clicked item
        object item = list.Items[itemIndex];
    }
}

EDIT ? Google - , , , , MouseDoubleClick. , ...

+7

All Articles