2010-05-21 141 views
4

我在vs2005和vs2008中发现了适用于我的CollapseAll宏。然而,这一半的方式在vs2010中有效。它看起来只会折叠顶层节点而不是可能扩展的任何子节点?有任何想法吗?宏在解决方案中全部崩溃visual studio 2010

谢谢, 杆。

Sub CollapseAll() 
     ' Get the the Solution Explorer tree 
     Dim UIHSolutionExplorer As UIHierarchy 
     UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object() 
     ' Check if there is any open solution 
     If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then 
      ' MsgBox("Nothing to collapse. You must have an open solution.") 
      Return 
     End If 
     ' Get the top node (the name of the solution) 
     Dim UIHSolutionRootNode As UIHierarchyItem 
     UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1) 
     UIHSolutionRootNode.DTE.SuppressUI = True 
     ' Collapse each project node 
     Dim UIHItem As UIHierarchyItem 
     For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems 
      'UIHItem.UIHierarchyItems.Expanded = False 
      If UIHItem.UIHierarchyItems.Expanded Then 
       Collapse(UIHItem) 
      End If 
     Next 
     ' Select the solution node, or else when you click 
     ' on the solution window 
     ' scrollbar, it will synchronize the open document 
     ' with the tree and pop 
     ' out the corresponding node which is probably not what you want. 
     UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect) 
     UIHSolutionRootNode.DTE.SuppressUI = False 
    End Sub 

    Private Sub Collapse(ByVal item As UIHierarchyItem) 
     For Each eitem As UIHierarchyItem In item.UIHierarchyItems 
      If eitem.UIHierarchyItems.Expanded AndAlso eitem.UIHierarchyItems.Count > 0 Then 
       Collapse(eitem) 
      End If 
     Next 
     item.UIHierarchyItems.Expanded = False 
    End Sub 
End Module 
+1

我可以请你重新考虑你接受的答案吗?你投的那个人可能已经解决了你的特定问题,但它并没有真正回答你的问题。然而我提供的那个确实。标记是这样帮助我的代表,毕竟,代表是这个网站的基础。谢谢! :) – MarqueIV 2013-06-21 19:32:19

回答

8

(是的,我知道你打上对方的回答是公认的,但在技术上,这不是回答您的编码问题,虽然它确实给了你一个答案,但这是你最初的要求。)

其实,我不认为这可能会在Visual Studio的任何版本中工作,因为你的Collapse函数中的逻辑是错误的。与项目中的初始循环相同(我不确定为什么你只是没有将解决方案节点传递给崩溃函数......)

具体而言,您正在检查它是否在钻入之前展开给孩子们,所以如果一个文件夹被折叠,但其孩子被展开,它不会进入并折叠孩子,因为您的支票会导致孩子被跳过。只需检查计数。毕竟你想要深入到一切崩溃。

这是我用你创建的版本作为起点。我还增加了重新选择所选项目(如果有的话)的功能。我喜欢选择正在处理的项目。如果我想要一切崩溃,我可以选择根节点,也可以取消选择所有内容,然后选择根节点(解决方案节点)。

由于我不是消息框的粉丝,我还添加了MultiBeep功能以进行声音反馈。 2声蜂鸣成功,3意味着您没有打开解决方案。

Sub CollapseSolutionTree() 

    ' Get the the Solution Explorer tree 
    Dim SolutionExplorer As UIHierarchy = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object() 

    ' Check if there is any open solution 
    If (SolutionExplorer.UIHierarchyItems.Count = 0) Then 
     MultiBeep(3) 
     Return 
    End If 

    ' Get the selected node (if any) 
    Dim SelectedNode As UIHierarchyItem 
    Try 
     SelectedNode = SolutionExplorer.SelectedItems(0) 
    Catch 
    End Try 

    ' Get the top node (the name of the solution) 
    Dim SolutionNode As UIHierarchyItem = SolutionExplorer.UIHierarchyItems.Item(1) 
    SolutionNode.DTE.SuppressUI = True 

    ' Collapse the solution tree 
    CollapseSolutionExplorerNode(SolutionNode) 

    ' If there was a selected item before the command, re-select it. Otherwise select the solution node. 
    If SelectedNode isnot Nothing 
     SelectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect) 
    Else 
     SolutionNode.Select(vsUISelectionType.vsUISelectionTypeSelect) 
    End If 

    SolutionNode.DTE.SuppressUI = False 

    MultiBeep(2) 

End Sub 

Private Sub CollapseSolutionExplorerNode(ByVal Folder As UIHierarchyItem) 

    For Each Subfolder As UIHierarchyItem In Folder.UIHierarchyItems 
     If Subfolder.UIHierarchyItems.Count Then CollapseSolutionExplorerNode(Subfolder) 
    Next 

    Folder.UIHierarchyItems.Expanded = False 

End Sub 

Private Sub MultiBeep(Count As Integer, Optional Spacing As Integer = 100) 
    Beep 
    For I = 2 to Count 
     System.Threading.Thread.CurrentThread.Sleep(Spacing) 
     Beep 
    Next 
End Sub 

HTH,

马克

+1

同意你的意见。 '(是的,我知道你已经接受了另一个答案,但从技术上讲,这并不能回答你的编码问题,尽管它确实给了你一个答案,不过这是你最初的要求)。感谢它帮助了我。我不想为此添加一个插件并进一步减慢我的VS。 – IsmailS 2011-11-02 06:37:47

+0

谢谢。这是他们没有的耻辱。 Rep是这个网站的基础,其他答案没有真正回答编程问题,只是展示了一种达到预期结果的不同方式。我的问题是,这不会帮助其他人想自己编写代码,因此我的答案。 – MarqueIV 2011-11-16 10:30:48

0

此功能是免费的Microsoft Productivity Power Tools扩展的Visual Studio 11的一部分,将有可能为在Visual Studio的下一个版本的标准功能发现为好。

+0

它不是生产力工具扩展的一部分。它是[Power Commands]的一部分(http://visualstudiogallery.msdn.microsoft.com/e5f41ad9-4edc-4912-bca3-91147db95b99/) – IsmailS 2011-11-02 06:43:55

相关问题