EnvironmentEvent macro does not end

I work in Visual Studio 2008, and I would like to have Edit> Outlining> Collapse to Definitions run whenever I open a file. It would be nice if after that all regions were expanded. I tried the code that Kiralessa suggested in the comment. There is a problem with code folding , and it works very well as a macro that I need to run manually. I tried to extend this macro to act as an event by placing the following code in the EnvironmentEvents module in the Macro IDE:

Public Sub documentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions") DTE.SuppressUI = True Dim objSelection As TextSelection = DTE.ActiveDocument.Selection objSelection.StartOfDocument() Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText) Loop objSelection.StartOfDocument() DTE.SuppressUI = False End Sub 

However, this is not like when I open a file from my solution in VS. To verify that the macro was running, I put the MsgBox() operator in this routine and noticed that the code before Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions") worked fine, but nothing seemed to get after this line. When I debugged and set a breakpoint in the routine, I would hit F10 to go to the next line, and control would leave the routine as soon as this ExecuteCommand line was run. Despite this, this line does not seem to do anything, i.e. It does not destroy the description.

I also tried using only DTE.ExecuteCommand("Edit.CollapsetoDefinitions") inside the routine, but no luck.

This question tries to get the same end result as this one , but I ask what I can do wrong in my event processing macro.

+1
macros event-handling visual-studio-2008 outlining
source share
1 answer

The problem is that the document is inactive when the event fires. One solution is to use the "once" timer to execute a short code delay after the DocumentOpened event:

 Dim DocumentOpenedTimer As Timer Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened DocumentOpenedTimer = New Timer(AddressOf ExpandRegionsCallBack, Nothing, 200, Timeout.Infinite) End Sub Private Sub ExpandRegionsCallBack(ByVal state As Object) ExpandRegions() DocumentOpenedTimer.Dispose() End Sub Public Sub ExpandRegions() Dim Document As EnvDTE.Document = DTE.ActiveDocument If (Document.FullName.EndsWith(".vb") OrElse Document.FullName.EndsWith(".cs")) Then If Not DTE.ActiveWindow.Caption.ToUpperInvariant.Contains("design".ToUpperInvariant) Then Document.DTE.SuppressUI = True Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions") Dim objSelection As TextSelection = Document.Selection objSelection.StartOfDocument() Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText) Loop objSelection.StartOfDocument() Document.DTE.SuppressUI = False End If End If End Sub 

I have not tested it extensively, so there may be some errors ... In addition, I added a check to make sure that the active document is the source code of C # or VB (not verified with VB) and that it is not in development mode.
Anyway, hope this works for you ...

+4
source share

All Articles