简体   繁体   English

在C#中的列表框项中添加右键单击/上下文菜单

[英]Adding a right click / context menu to listbox items in C#

I have a ListBox, and its populated with items, id like to know how to: 我有一个ListBox,并且其中装有项,id想知道如何:

  1. when you rightclick in the listbox, that the rightclicked item will be selected, 当您在列表框中右键单击时,将选择右键单击的项目,
  2. a rightclick menu will be displayed with several items.. 将会显示一个右键菜单,其中包含多个项目。
  3. when you click on any of the items, a corresponding void will be triggered.. 当您单击任何项​​目时,将触发相应的空白。

thanks in advance for any help, and code examples please! 在此先感谢您的帮助,并提供示例代码!

This feels to me like a "homework" question, so I am going to answer by (I hope) giving you just a few pointers to solving this for yourself. 在我看来,这就像一个“作业”问题,因此我将(希望)为您提供一些自己解决问题的指导。

Phase One 第一阶段

  1. create a sample project with a ListBox 用ListBox创建一个示例项目
  2. define event handlers for MouseClick, MouseDown, and Click events. 为MouseClick,MouseDown和Click事件定义事件处理程序。
  3. put a Console.WriteLine("some appropriate text"); 放置一个Console.WriteLine(“一些适当的文本”); statement in each of those handlers so you can look in the Output Window in Visual Studio and see which event handler was called. 每个处理程序中的语句,以便您可以在Visual Studio的“输出窗口”中查看并查看调用了哪个事件处理程序。

... ...

Phase Two 第二阶段

  1. run the test program and observe the difference between what events are reported for a left mouse down and a right-mouse down (assuming your environment has the context click set to the right mouse down ... which may not be true for everyone). 运行测试程序,观察鼠标左键按下和鼠标右键按下时报告的事件之间的差异(假设您的环境将上下文单击设置为鼠标右键...可能并非每个人都正确)。

  2. focus on the one event you can intercept with a context-click. 专注于可以通过上下文单击来拦截的一个事件。

  3. add a context menu to the test project and set that context menu to be the context menu of the ListBox. 将上下文菜单添加到测试项目,并将该上下文菜单设置为ListBox的上下文菜单。

  4. verify that you can now right click on an item in the ListBox and that the context menu will appear, BUT THE EVENT IS STILL HANDLED BY THE HANDLER YOU "DISCOVERED" IN STEP 2. 验证您现在可以右键单击ListBox中的一个项目,并显示上下文菜单,但该事件仍由您在步骤2中“发现”的处理程序处理。

  5. now go through all the Event handlers for ListBox, and figure out which one could be used to detect, given a certain location in the ListBox, which List Item was "hit." 现在,遍历ListBox的所有事件处理程序,并在给定ListBox中的特定位置的情况下,找出可以用来检测哪个列表项被“命中”。

  6. once you can determine which List Item was right-clicked on, and you know your context menu is working, you have only the problem of making sure that the right-clicked List Item is selected : and that's pretty easy. 一旦您可以确定右键单击了哪个列表项,并且知道上下文菜单正在工作,就只需要确保选择了右键单击的列表项就可以了:这很容易。

Figuring this out yourself will teach you several very useful things you'll be able to use later in programming to other controls. 自己弄清楚这一点将教给您一些非常有用的知识,您以后可以在对其他控件进行编程时使用它们。

best of luck, Bill 祝你好运,比尔

First, you need to subscribe to ListBox.MouseClick event. 首先,您需要订阅ListBox.MouseClick事件。 You will be able do determine what button was pressed and cursor coordinates. 您将能够确定按下了哪个按钮以及光标坐标。 Then, use ListBox.IndexFromPoint method to find clicked item. 然后,使用ListBox.IndexFromPoint方法查找单击的项目。 You can select it using ListBox.SelectedIndex property. 您可以使用ListBox.SelectedIndex属性选择它。 To display context menu use ContextMenu or ContextMenuStrip classes. 要显示上下文菜单,请使用ContextMenu或ContextMenuStrip类。 Additional documentation on context menu is available in MSDN MSDN中提供了有关上下文菜单的其他文档

1.when you right click in the listbox, that the right clicked item will be selected 1.在列表框中单击鼠标右键时,将选择右键单击的项目

2.a right click menu will be displayed with several items.. 2.将显示一个右键菜单,其中包含多个项目。

private void listBoxNode_MouseUp(object sender, MouseEventArgs e)
    {
        int location = listBoxNode.IndexFromPoint(e.Location);
        if (e.Button == MouseButtons.Right)
        {
            listBoxNode.SelectedIndex = location;                //Index selected
            contextMenuStrip1.Show(PointToScreen(e.Location));   //Show Menu
        }
    }

3.when you click on any of the items, a corresponding void will be triggered.. 3.单击任何项​​目时,将触发相应的空白。

private void showDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
              //put your code here after clicking
              //on items in context menu
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM