c# mouse left click on listbox

How can I execute an function when mouse left click on a Item in listbox ? I Can't use SelectChanged because I listen also right click, and when I right click on the item it's execute the fuction SelectChanged also.

Or how to detect in SelectChange method, if event it's right click or left

2 answers

  • answered 2018-08-09 00:56 Evertude

    taken from Right Click to select items in a ListBox

    this.ListBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.List_LeftClick);
    
    private void List_LeftClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int index = this.listBox.IndexFromPoint(e.Location);
            if (index != ListBox.NoMatches)
            {
                // Do something
            }
        }
    }
    

  • answered 2018-08-09 13:49 emeliku

    listBoxG.AddHandler(UIElement.MouseLeftButtonUpEvent, new RoutedEventHandler(OnMouseLeftButtonUp_listBoxG), true);
    
    public void OnMouseLeftButtonUp_listBoxG(Object sender, RoutedEventArgs e)
    {
    // something
    }