【问题标题】:How to get selected text of visual studio 2015 editor windows?如何获取 Visual Studio 2015 编辑器窗口的选定文本?
【发布时间】:2016-11-09 21:31:25
【问题描述】:

我想实现一个 Visual Studio 2015 扩展来获取用户在代码编辑器中选择的文本。比我想操纵选定的文本。

A 在代码编辑器中通过上下文菜单有一个按钮/命令。但我不知道如何 获取选定的文本。

我认为这个解决方案here 已经过时或者我误解了这个解决方案。

【问题讨论】:

    标签: c# visual-studio visual-studio-2015 visual-studio-extensions


    【解决方案1】:

    我假设您的代码已经在派生自 Package 的类中。

    您可以像这样获取和修改选择文本:

    DTE dte = (DTE)GetService(typeof(DTE));
    
    if (dte.ActiveDocument != null)
    {
        var selection = (TextSelection)dte.ActiveDocument.Selection;
        string text = selection.Text;
    
        // Modify the text, for example:
        text = ">>" + text + "<<";
    
        // Replace the selection with the modified text.
        selection.Text = text;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 2015-09-10
      • 1970-01-01
      • 2016-11-14
      • 2016-06-15
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多