I am currently trying to create an extension for this using “Visual Studio 2015”. I want to create a function that inserts the source code that fits into the cursor part of the current code editor when the button of ToolWindow is clicked. There was a ToolWindow sample in the SDK template. However, it was completed in ToolWindow, and no way to access the code editor was written. How can I get / set the current code editor source code from ToolWindow?
As Sergey suggests, we can use the TextSelection.Text property.
But somehow I failed to get the Selection in this way for VS2015:DTE.ActiveDocument.Selection.
For me, In VS2015 I created a new custom Tool Window item, added a simple TextBox control in the ToolWindow, and then double-click the default Click me! button to navigate to button-Click method in ToolWindow1Control.xaml.cs file, after that I add code like below to get text from textbox and insert it into editor:
[SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Justification = "Sample code")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Default event handler naming pattern")]
private void button1_Click(object sender, RoutedEventArgs e)
{
string text = textBox.Text;
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
(dte.ActiveDocument.Selection as EnvDTE.TextSelection).Text = text;
}
So for me, I actually use code below to insert the text:
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
(dte.ActiveDocument.Selection as EnvDTE.TextSelection).Text = "Add your text here.";
In addition:
To use the code above, you need to add definitions in ToolWindow1Control.xaml.cs file:
using Microsoft.VisualStudio.Shell;
using EnvDTE;
Hope it makes some help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With