CSharp_Heaven

Hi All,

I would like to know how to collapse newly created #regions right after I create them. I have seen this can be done but I cannot figure out how to do this. I've been pulling my hair for months now. I know what cause this but I can't fingure out how to overcome the problem.

When the #region is created it takes time the outlining box appears next to it and while the document is being updated I won't be able to collapse the #region. Only when the document's outline boxes appear next to their #regions I'm able to collapse regions at ease.

I've been searching for months and couldn't find a way to be notified when the document outline (the boxes) have been drawn so I can start collapsing the regions.

I don't want to stop outlining the document and then turn it on again before trying to collapse the #region this would cause a huge headache if there are other classes with regions in the same code file.

I would appreciate it if someone to come up with a solution please.

Thanks



Re: Visual C# IDE Yet another #region question

Ken_L

I'm pretty sure you make a Visual Studio macro






Re: Visual C# IDE Yet another #region question

Ken_L

And here it is

http://www.knowdotnet.com/articles/toggleallregions.html

Public Sub CloseAllRegions()
ToggleAllRegions("C")
End Sub
Public Sub OpenAllRegions()
ToggleAllRegions("O")
End Sub
Public Sub ToggleAllRegions(ByVal Switch As String)
' in walking up through the code, if we do not see
' #endregion/end region before we get to the
' #region, then the region was closed, otherwise
' it was already open, and each time a #region is
' found, set open = false as we are only looking
' for closed regions and if we toggle an open region
' it will close...
Dim cmd As String = "Edit.ToggleOutliningExpansion"

Try
Dim open As Boolean = False
Dim ts As TextSelection = DTE.ActiveDocument.Selection
ts.EndOfDocument()
Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
Dim line As String

Do While Not ep.AtStartOfDocument
ts.StartOfLine()
ts.LineUp(
True)
line = ts.Text.ToLower.Trim
If line.StartsWith("#end region") Or _
line.StartsWith("#endregion")
Then
open = True
ElseIf line.StartsWith("#region ") Then
If Switch = "C" Then
If open Then
DTE.ExecuteCommand(cmd)
End If
Else
If Not open Then
DTE.ExecuteCommand(cmd)
End If
End If
open = False
End If
ep = ts.ActivePoint.CreateEditPoint
Loop
Catch
End Try
End Sub





Re: Visual C# IDE Yet another #region question

CSharp_Heaven

Hi Ken_L,

Thank you for replying.

If you create the regions (programmatically) and try to collapse the regions at same time not only the code snippet you provided but also my own rountines won't work either.

If I to create dozens of regions and try to collapse them using the ExecuteCommand method with "Edit.ToggleOutliningExpansion" as the parameter all regions will remain expanded.

I can start collapsing the regions after the previous updates (region creation) were done to the document and the document shows the (+[|-)] boxes next to each #region.

So basically I'm looking for a way to kick start the collpasing process after the (+[|-)] appears or redrawn on the same document.

Thanks





Re: Visual C# IDE Yet another #region question

Geoff.Cox

Edit.ToggleOutlineExpansion has a shortcut key Ctrl-M, Ctrl-M that you could use right after the + appears.

To speed up the + appearing, you coud use the "surround with" refactoring so that the IDE doesn't have to search for the previous #region when you type #endregion.






Re: Visual C# IDE Yet another #region question

CSharp_Heaven

Hi Geoff,

I won't be typing the regions by hand. I'm creating the regions programmatically.

Anyways I have given up for now and I changed my rountines to collapse the entire code file after new regions has been inserted programmatically. You see when I "Stop Outlining" and then restart the "Automatic Outling" through my rountines then the new regions also get their + boxes.

It will annoy some users that other regions in other classes in same code file also collapsed. I can't please everyone, can I

Cheers