简体   繁体   English

如何为选定的文本提供上下文菜单? 和

[英]How can I have context menu for selected text? &

2 problems: 2个问题:

  1. 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 ?. 怎么样 ?。

  2. For copying text user has 3 options: 对于复制文本,用户有3个选项:

    1. Copy (in context menu) 复制(在上下文菜单中)
    2. Edit menu 编辑菜单
    3. Ctrl+C Ctrl + C

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.

相关问题 我有一个由6个不同标签共享的上下文菜单,如何判断哪个标签正在使用上下文菜单的当前实例? - I have a context menu which is being shared by 6 different labels, how can I tell which label is using the current instance of the context menu? 如何禁用树视图的上下文菜单? - How can I disable the context menu of a treeview? 如何向 ListBoxItem 添加上下文菜单? - How can I add a context menu to a ListBoxItem? 无法获得所选项目的上下文菜单 - Can't get selected item context menu 仅当鼠标位置在richTextBox中的选定文本上时,才能启用上下文菜单菜单吗? - How can i enable contextmenu menu only if the mouse position is on selected text in richTextBox? 如何从上下文菜单中获取所选项目 - How to get the selected item from a context menu 从上下文菜单更改RichTextBox中选定文本的前景色 - Change foreground color of selected text in RichTextBox from context menu Outlook VSTO 加载项在邮件主题中选择文本和上下文菜单 - Outlook VSTO Add-in selected text and context menu in Mail subject 我如何在DataGridView中的C#中创建上下文菜单 - How can i create a context menu in c# in a datagridview 如何解决Treeview单击的上下文菜单问题 - How can i fix the issue for context menu for Treeview click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM