vb.net 右键单击:菜单选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5506811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right click: menu options
提问by Voldemort
there is a feature I'd like to implement in my application:
我想在我的应用程序中实现一个功能:
The user right clicks on my picturebox object. Good. When that happens, some code of mine executes and will generate a list of options. Then a menu appears where the mouse right-clicked, made up of these options. When the user clicks on one of these options, the menu is deleted and some code is run given the option index as parameter.
用户右键单击我的图片框对象。好的。当这种情况发生时,我的一些代码会执行并生成一个选项列表。然后在鼠标右键单击的位置出现一个菜单,由这些选项组成。当用户单击这些选项之一时,菜单将被删除,并在将选项索引作为参数的情况下运行一些代码。
My two problems:
我的两个问题:
- How can I tell when the user right clicks? I can see an event handler for "click", but that includes left clicks....
- How do I create one of these menus? I mean, go ahead and right click something. That's the kind of menu I'm looking for.
- 如何判断用户何时右键单击?我可以看到“单击”的事件处理程序,但其中包括左键单击....
- 如何创建这些菜单之一?我的意思是,继续并右键单击某些内容。这就是我正在寻找的那种菜单。
回答by Hans Passant
You need to implement the picturebox' MouseUp event. Check if the right button was clicked, then create a ContextMenuStrip with the menu items you want. You can use, say, the Tag property of the items you add to help identify them so you can give them a common Click event handler. Like this:
您需要实现图片框的 MouseUp 事件。检查是否单击了右侧按钮,然后使用您想要的菜单项创建一个 ContextMenuStrip。例如,您可以使用您添加的项目的 Tag 属性来帮助识别它们,这样您就可以为它们提供一个通用的 Click 事件处理程序。像这样:
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
Dim item1 = cms.Items.Add("foo")
item1.Tag = 1
AddHandler item1.Click, AddressOf menuChoice
Dim item2 = cms.Items.Add("bar")
item2.Tag = 2
AddHandler item2.Click, AddressOf menuChoice
'-- etc
'..
cms.Show(PictureBox1, e.Location)
End Sub
Private Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
'-- etc
End Sub
回答by Edurne Pascual
To your first question: you actually handle just the "click" event, there is no separate event for right-click. But look at the EventArgs object you get passed for the event: it includes information of which button was pressed (and would give you more info if a mouse click had anything beyond that). So you check the button within an if
block, and you're good to go.
对于您的第一个问题:您实际上只处理“单击”事件,右键单击没有单独的事件。但是看看你为事件传递的 EventArgs 对象:它包括按下哪个按钮的信息(如果鼠标点击还有其他任何东西,它会为你提供更多信息)。所以你检查一个if
块内的按钮,你就可以开始了。
To your second question: http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx. If your menu is pre-defined, just look for that component on the Designer and prepare the menu from there and call its Show()
method from the click handler. If you need to decide the menu entries on the fly, the linked documentation page actually includes an example on that ;)
你的第二个问题:http: //msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx。如果您的菜单是预定义的,只需在设计器上查找该组件并从那里准备菜单并Show()
从单击处理程序调用其方法。如果您需要即时决定菜单项,链接的文档页面实际上包含一个示例;)
PS: oops, I just noticed Jon's comment on the question. The answer I gave you is for Windows Forms. If you are on WPF let us know and I'll update with the details (although the concepts ain't too different).
PS:哎呀,我刚刚注意到乔恩对这个问题的评论。我给你的答案是针对 Windows 窗体的。如果您使用 WPF,请告诉我们,我会更新详细信息(尽管概念并没有太大不同)。
回答by Coridex73
There is actually an easier way to do this. Double-click on the control you wish to be able to right click. Now go to the top of the page and it should say in comboboxes; 'Control' and 'Click' Click on the 'click' combobox and look for: Right-Click. Use a ContextMenuStrip for your right click menu.
实际上有一种更简单的方法可以做到这一点。双击您希望能够右键单击的控件。现在转到页面顶部,它应该在组合框中显示;“控制”和“单击”单击“单击”组合框并查找:右键单击。使用 ContextMenuStrip 作为您的右键菜单。
Now you can choose which function you want.
现在您可以选择您想要的功能。
Private Sub PictureBox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.RightClick
ContextMenuStrip1.Show()
MsgBox("Content Activated.", MsgBoxStyle.Information, "Success!")
End Sub
Hope I could help. :)
希望我能帮上忙。:)
Coridex73
Coridex73