2010-05-21 143 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

相關問題