Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From a VS2008 VSPackage, how do I get notified whenever caret position changed?

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.

like image 296
Omer Raviv Avatar asked May 15 '13 06:05

Omer Raviv


1 Answers

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
   }
}
like image 53
Philip Pittle Avatar answered Sep 28 '22 02:09

Philip Pittle