C# How to select ListBox item with a RightClick?

∥☆過路亽.° 提交于 2019-12-12 14:22:30

问题


I have tried a lot of methods for this and done hours of research, but it just never seems to work for me.

This is my current code, and I don't know why it shouldn't work.

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
        if (e.Button == MouseButtons.Right)
        {
            contextMenuStrip1.Show();
        }
    }

Also I don't care about the context menu that can be removed I am just looking for a way to make the right mouse button select the item I click on.

Any Ideas?


回答1:


You are close, you just forgot to select the item. Fix:

    private void listBox1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            var item = listBox1.IndexFromPoint(e.Location);
            if (item >= 0) {
                listBox1.SelectedIndex = item;
                contextMenuStrip1.Show(listBox1, e.Location);
            }
        }
    }



回答2:


  private void lstFiles_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)   //(1)
        {
            int indexOfItemUnderMouseToDrag;
            indexOfItemUnderMouseToDrag = lstFiles.IndexFromPoint(e.X, e.Y); //(2)
            if (indexOfItemUnderMouseToDrag != ListBox.NoMatches)
            {
                lstFiles.SelectedIndex = indexOfItemUnderMouseToDrag; //(3)
            }
        }
    }



回答3:


Each control inherits ContextMenu property from Control class. Assign your context menu object to the ContextMenu property of your list box control and WinForms will handle it automatically for you.




回答4:


    private void listBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button== MouseButtons.Right)
        {
            int nowIndex = e.Y / listBox1.ItemHeight;
            if (nowIndex < listBox1.Items.Count)
            {
                listBox1.SelectedIndex = e.Y / listBox1.ItemHeight;
            }
            else
            {
                //Out of rang
            }
        }
    }

I do not know much in C#, but I tried :)




回答5:


I was dealing with the same issue. From Hans Passant's reply I tweaked it a little to get the below code. I also found that I didn't need to put contextMenuStrip1.Show(listBox1, e.Location); in there at all. It was automatically called for me.

(I'm using Visual Studio 2010 Ultimate with and compiling at .NET 4. I also verified that the below code works for BOTH MouseUp and MouseDown.)

    private void OnMouseDown(object sender, MouseEventArgs args)
    {
        if (args.Button == MouseButtons.Right)
        {
            var item = this.IndexFromPoint(args.Location);
            if (item >= 0 && this.SelectedIndices.Contains(item) == false)
            {
                this.SelectedItems.Clear();
                this.SelectedIndex = item;
            }
        }
    }


来源:https://stackoverflow.com/questions/9229034/c-sharp-how-to-select-listbox-item-with-a-rightclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!