[英]How can I have context menu for selected text? &
2 problems: 2个问题:
In google chrome if you select a word (say problem
) and then right click on this selected text, the context menu shows two items 1. Copy 2.Search google for problem
3. Inspect element. 在谷歌浏览器中,如果您选择一个单词(例如
problem
),然后右键单击此选定的文本,则上下文菜单显示两个项目:1.复制2.搜索谷歌以查找problem
3.检查元素。 The context menu is different from the context menu of that entire window. 上下文菜单与整个窗口的上下文菜单不同。 How can I have this separate context menu for selected text.
如何为所选文本提供单独的上下文菜单。
The exact task I'm trying to accomplish is: I've a textbox (in winforms). 我要完成的确切任务是:我有一个文本框(在winforms中)。 Now when user rt clicks, the context menu show just
paste
. 现在,当用户rt单击时,上下文菜单将显示为
paste
。 If text box is filled has some text and user selects some text and then right clicks on selected text, It should show context menu with items: copy, cut, paste, select all. 如果文本框中填充了一些文本,并且用户选择了一些文本,然后右键单击所选文本,则应显示上下文菜单,其中包含以下项目:复制,剪切,粘贴,全选。 How ?.
怎么样 ?。
For copying text user has 3 options: 对于复制文本,用户有3个选项:
all these does same thing, copies selected data to clipboard. 所有这些都做同样的事情,将选定的数据复制到剪贴板。 I want to overwrite functionality of copying selected data using these 3 methods to copying desired data to clipboard.
我想使用以下3种方法覆盖将所选数据复制到剪贴板的复制所选数据的功能。 How?
怎么样?
You can assign a custom ContextMenuStrip to the TextBox's ContextMenuStrip property. 您可以将自定义ContextMenuStrip分配给TextBox的ContextMenuStrip属性。 Thus I'd instantiate my own, populate it with items for copy/paste and the other items you need.
因此,我将实例化自己的实例,在其中填充用于复制/粘贴的项目以及您需要的其他项目。 Then, you can handle the ContextMenuStrip's Opening event, checking to see if there is selected text in the TextBox and modifying the menu's items just before it is shown (ie change your "Search google" item's Visible property).
然后,您可以处理ContextMenuStrip的Opening事件,检查在TextBox中是否有选定的文本,并在显示菜单之前修改菜单项(即更改“ Search google”项的Visible属性)。
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { var item = searchGoogleMenuItem; if (item.Visible = !string.IsNullOrEmpty(textBox1.SelectedText)) item.Text = string.Format("Search Google '{0}'", textBox1.SelectedText); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.