TreeView.CollapseAll メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TreeView.CollapseAll メソッドの意味・解説 

TreeView.CollapseAll メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

ツリー内のすべてのノード閉じます

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Dim instance As TreeView

instance.CollapseAll
public void CollapseAll ()
public:
void CollapseAll ()
public void CollapseAll ()
解説解説
使用例使用例

CollapseAll メソッド使用して、TreeView コントロール内のすべてのノード同時に折りたたむ方法次のコード例示します

<%@ Page Language="VB" %>

<script runat="server">

  Sub Button_Command(ByVal sender As
 Object, ByVal e As CommandEventArgs)

    Select Case e.CommandName

      Case "Expand"
        LinksTreeView.ExpandAll()

      Case "Collapse"
        LinksTreeView.CollapseAll()

      Case Else
        ' Do nothing.

    End Select

  End Sub

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeView ExpandAll and CollapseAll Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10"
 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5"
 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5"
 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10"
 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            SelectAction="None">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>
      
      <hr>
      
      <asp:Button id="ExpandButton" 
        CommandName="Expand"
        Text="Expand All Nodes"
        OnCommand="Button_Command"
        runat="server"/>
         
      &nbsp;
      
      <asp:Button id="CollapseButton" 
        CommandName="Collapse"
        Text="Collapse All Nodes"
        OnCommand="Button_Command"
        runat="server"/>

    </form>
  </body>
</html>

<%@ Page Language="C#" %>

<script runat="server">

  void Button_Command(Object sender, CommandEventArgs e)
  {

    switch(e.CommandName)
    {

      case "Expand":
        LinksTreeView.ExpandAll();
        break;
      case "Collapse":
        LinksTreeView.CollapseAll();
        break;
      default:
        // Do nothing.
        break;

    }

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeView ExpandAll and CollapseAll Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            SelectAction="None">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>
      
      <hr>
      
      <asp:Button id="ExpandButton" 
        CommandName="Expand"
        Text="Expand All Nodes"
        OnCommand="Button_Command"
        runat="server"/>
         
      &nbsp;
      
      <asp:Button id="CollapseButton" 
        CommandName="Collapse"
        Text="Collapse All Nodes"
        OnCommand="Button_Command"
        runat="server"/>

    </form>
  </body>
</html>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TreeView クラス
TreeView メンバ
System.Web.UI.WebControls 名前空間
ExpandAll

TreeView.CollapseAll メソッド

すべてのツリー ノード折りたたみます。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Dim instance As TreeView

instance.CollapseAll
public void CollapseAll ()
public:
void CollapseAll ()
public void CollapseAll ()
解説解説
使用例使用例

TreeView折りたたみの状態を変更してチェックされているノード表示する方法次のコード例示します。まず、すべてのノード折りたたまれ、BeforeExpand イベントイベント ハンドラ追加されます。次にすべてのノード展開されます。BeforeExpand イベント ハンドラは、指定したノードに、チェックされている子ノードがあるかどうか確認しますチェックされている子ノードない場合、そのノードの展開はキャンセルされます。次にノードの横にある正符号クリックされたときに通常のノード展開できるようにするため、BeforeExpand イベント ハンドラ削除されます。

BeforeCollapse イベント処理してこの動作実装することもできます詳細については、該当するトピックの例を参照してください

詳細については、CheckBoxes のリファレンス トピック参照してください

Private Sub showCheckedNodesButton_Click(ByVal
 sender As Object, ByVal
 e As EventArgs)
    ' Disable redrawing of treeView1 to prevent flickering 
    ' while changes are made.
    treeView1.BeginUpdate()

    ' Collapse all nodes of treeView1.
    treeView1.CollapseAll()

    ' Add the CheckForCheckedChildren event handler to the BeforeExpand
 event.
    AddHandler treeView1.BeforeExpand, AddressOf
 CheckForCheckedChildren

    ' Expand all nodes of treeView1. Nodes without checked children
 are 
    ' prevented from expanding by the checkForCheckedChildren event
 handler.
    treeView1.ExpandAll()

    ' Remove the checkForCheckedChildren event handler from the BeforeExpand
 
    ' event so manual node expansion will work correctly.
    RemoveHandler treeView1.BeforeExpand, AddressOf
 CheckForCheckedChildren

    ' Enable redrawing of treeView1.
    treeView1.EndUpdate()
End Sub 'showCheckedNodesButton_Click

' Prevent expansion of a node that does not have any checked child nodes.
Private Sub CheckForCheckedChildren(ByVal
 sender As Object, ByVal
 e As TreeViewCancelEventArgs)
    If Not HasCheckedChildNodes(e.Node) Then
        e.Cancel = True
    End If
End Sub 'CheckForCheckedChildren

' Returns a value indicating whether the specified 
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal
 node As TreeNode) As Boolean
    If node.Nodes.Count = 0 Then
        Return False
    End If
    Dim childNode As TreeNode
    For Each childNode In
 node.Nodes
        If childNode.Checked Then
            Return True
        End If
        ' Recursively check the children of the current child node.
        If HasCheckedChildNodes(childNode) Then
            Return True
        End If
    Next childNode
    Return False
End Function 'HasCheckedChildNodes
private void showCheckedNodesButton_Click(object
 sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering 
    // while changes are made.
    treeView1.BeginUpdate();

    // Collapse all nodes of treeView1.
    treeView1.CollapseAll();

    // Add the checkForCheckedChildren event handler to the BeforeExpand
 event.
    treeView1.BeforeExpand += checkForCheckedChildren;

    // Expand all nodes of treeView1. Nodes without checked children
 are 
    // prevented from expanding by the checkForCheckedChildren event
 handler.
    treeView1.ExpandAll();

    // Remove the checkForCheckedChildren event handler from the BeforeExpand
 
    // event so manual node expansion will work correctly.
    treeView1.BeforeExpand -= checkForCheckedChildren;

    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
}

// Prevent expansion of a node that does not have any checked child
 nodes.
private void CheckForCheckedChildrenHandler(object
 sender, 
    TreeViewCancelEventArgs e)
{
    if (!HasCheckedChildNodes(e.Node)) e.Cancel = true;
}

// Returns a value indicating whether the specified 
// TreeNode has checked child nodes.
private bool HasCheckedChildNodes(TreeNode
 node)
{
    if (node.Nodes.Count == 0) return false;
    foreach (TreeNode childNode in node.Nodes)
    {
        if (childNode.Checked) return true;
        // Recursively check the children of the current child node.
        if (HasCheckedChildNodes(childNode)) return
 true;
    }
    return false;
}
private:
   void showCheckedNodesButton_Click( Object^ /*sender*/, EventArgs^
 /*e*/ )
   {
      // Disable redrawing of treeView1 to prevent flickering 
      // while changes are made.
      treeView1->BeginUpdate();
      
      // Collapse all nodes of treeView1.
      treeView1->CollapseAll();
      
      // Add the checkForCheckedChildren event handler to the BeforeExpand
 event.
      treeView1->BeforeExpand += checkForCheckedChildren;
      
      // Expand all nodes of treeView1. Nodes without checked children
 are 
      // prevented from expanding by the checkForCheckedChildren event
 handler.
      treeView1->ExpandAll();
      
      // Remove the checkForCheckedChildren event handler from the BeforeExpand
 
      // event so manual node expansion will work correctly.
      treeView1->BeforeExpand -= checkForCheckedChildren;
      
      // Enable redrawing of treeView1.
      treeView1->EndUpdate();
   }

   // Prevent expansion of a node that does not have any checked child
 nodes.
   void CheckForCheckedChildrenHandler( Object^ /*sender*/, TreeViewCancelEventArgs^
 e )
   {
      if (  !HasCheckedChildNodes( e->Node ) )
            e->Cancel = true;
   }


   // Returns a value indicating whether the specified 
   // TreeNode has checked child nodes.
   bool HasCheckedChildNodes( TreeNode^ node )
   {
      if ( node->Nodes->Count == 0 )
            return false;

      System::Collections::IEnumerator^ myEnum = node->Nodes->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         TreeNode^ childNode = safe_cast<TreeNode^>(myEnum->Current);
         if ( childNode->Checked )
                  return true;

         // Recursively check the children of the current child node.
         if ( HasCheckedChildNodes( childNode ) )
                  return true;
      }

      return false;
   }
private void showCheckedNodesButton_Click(Object
 sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering 
    // while changes are made.
    treeView1.BeginUpdate();
    // Collapse all nodes of treeView1.
    treeView1.CollapseAll();
    // Add the checkForCheckedChildren event handler to the
    // BeforeExpand event.
    treeView1.add_BeforeExpand(checkForCheckedChildren);
    // Expand all nodes of treeView1. Nodes without checked children
 are 
    // prevented from expanding by the checkForCheckedChildren event
 handler.
    treeView1.ExpandAll();
    // Remove the checkForCheckedChildren event handler from
    // the BeforeExpand 
    // event so manual node expansion will work correctly.
    treeView1.remove_BeforeExpand(checkForCheckedChildren);
    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
} //showCheckedNodesButton_Click

// Prevent expansion of a node that does not have any checked child
 nodes.
private void CheckForCheckedChildrenHandler(Object
 sender,
    TreeViewCancelEventArgs e)
{
    if (!(HasCheckedChildNodes(e.get_Node()))) {
        e.set_Cancel(true);
    }
} //CheckForCheckedChildrenHandler

// Returns a value indicating whether the specified 
// TreeNode has checked child nodes.
private boolean HasCheckedChildNodes(TreeNode node)
{
    if (node.get_Nodes().get_Count() == 0) {
        return false;
    }
    for (int iCtr = 0; iCtr < node.get_Nodes().get_Count();
 iCtr++) {
        TreeNode childNode = node.get_Nodes().get_Item(iCtr);
        if (childNode.get_Checked()) {
            return true;
        }
        // Recursively check the children of the current child node.
        if (HasCheckedChildNodes(childNode)) {
            return true;
        }
    }
    return false;
} //HasCheckedChildNodes
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

TreeView.CollapseAll メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TreeView.CollapseAll メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS