Domanda

I'd like to get notified whenever the caret position changed in the active text view. The only thing EnvDTE seems to offer is the LineChanged event, which of-course does not get raised when moving the caret left or right within the same line.

I realize VS2010's Editor Extensibility lets you do this with no sweat, but I need a solution that is backwards compatible with VS2008.

È stato utile?

Soluzione 2

I found a solution. The solution is to create an IOleCommandTarget and register it on the IVsTextView (See the last two bits of code in this blog post (in Herbrew)). Then, each time a command is fired, I check whether the caret position has changed. See also: this blog post - How to intercept key presses in the Visual Studio text editor

Altri suggerimenti

Have you seen this: DTE2 events don't fire

You have to keep a local instance of the Events object, otherwise the event wont fire (I assume because the COM backed Events object went out of scope and was GC'd):

public class MyVSPackage
{ 
   TextEditorEvents _textEditorEvents;

   public MyVSPackage()
   {
        _textEditorEvents = DTE.Events.TextEditorEvents;

        _textEditorEvents.LineChanged += (point, endPoint, hint) => //Do something here
   }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top