TreeView.CheckBoxes プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TreeView.CheckBoxes プロパティの意味・解説 

TreeView.CheckBoxes プロパティ

ツリー ビュー コントロール内のツリー ノードの横にチェック ボックス表示するかどうかを示す値を取得または設定します

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

解説解説

チェック ボックスは、ツリー ノードラベル左側Image表示されている場合はその両方左側表示されます。チェック ボックス使用すると、ユーザー一度複数ツリー ノード選択できるようになります

TreeView の CheckBoxes プロパティtrue設定されており、かつ StateImageList プロパティ設定されている場合TreeView含まれる各 TreeNode には、チェックされていない状態であるかチェックされた状態であるかを示すために、StateImageList最初イメージ2 番目のイメージそれぞれ表示されます。ただし、次の状況発生すると、表示されるイメージが変わる場合あります。たとえば、ノードの SelectedImageIndex が 0 または 1 以外の値に設定されており、親 TreeViewCheckBoxes プロパティfalse設定されている場合SelectedImageIndex は、未設定状態を表す -1 に自動的にリセットされません。この場合指定されインデックス位置にある状態イメージ表示されます。CheckBoxes再度 true設定すると、チェックされた状態であるかチェックされていない状態であるかを示すために、StateImageList最初イメージ2 番目のイメージそれぞれ表示されます。

メモメモ

実行時CheckBoxes プロパティ設定すると、TreeView ハンドル再作成され (Control.RecreateHandle のトピック参照)、コントロール外観更新されます。これによって、選択されている TreeNode を除くすべてツリー ノード表示折りたたまれます。

使用例使用例

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

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

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Private treeView1 As TreeView
    Private showCheckedNodesButton As Button

    Public Sub New()
        treeView1 = New TreeView
        showCheckedNodesButton = New Button

        Me.SuspendLayout()

        ' Initialize treeView1.
        treeView1.Location = New Point(0, 25)
        treeView1.Size = New Size(292, 248)
        treeView1.Anchor = AnchorStyles.Top Or AnchorStyles.Left
 Or AnchorStyles.Bottom Or AnchorStyles.Right
        treeView1.CheckBoxes = True

        ' Add nodes to treeView1.
        Dim node As TreeNode
        Dim x As Integer
        For x = 0 To 3
            ' Add a root node.
            node = treeView1.Nodes.Add(String.Format("Node{0}",
 x * 4))
            Dim y As Integer
            For y = 1 To 4
                ' Add a node as a child of the previously added node.
                node = node.Nodes.Add(String.Format("Node{0}",
 x * 4 + y))
            Next y
        Next x

        ' Set the checked state of one of the nodes to
        ' demonstrate the showCheckedNodesButton button behavior.
        treeView1.Nodes(1).Nodes(0).Nodes(0).Checked = True

        ' Initialize showCheckedNodesButton.
        showCheckedNodesButton.Size = New Size(144, 24)
        showCheckedNodesButton.Text = "Show Checked Nodes"
        AddHandler showCheckedNodesButton.Click, AddressOf
 showCheckedNodesButton_Click

        ' Initialize the form.
        Me.ClientSize = New Size(292, 273)
        Me.Controls.AddRange(New Control()
 {showCheckedNodesButton, treeView1})

        Me.ResumeLayout(False)
    End Sub 'New

    <STAThreadAttribute()> _
    Shared Sub Main()
        Application.Run(New Form1)
    End Sub 'Main

    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

End Class 'Form1 
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    private TreeView treeView1;
    private Button showCheckedNodesButton;
    private TreeViewCancelEventHandler checkForCheckedChildren;

    public Form1()
    {
        treeView1 = new TreeView();
        showCheckedNodesButton = new Button();
        checkForCheckedChildren = 
            new TreeViewCancelEventHandler(CheckForCheckedChildrenHandler);

        this.SuspendLayout();

        // Initialize treeView1.
        treeView1.Location = new Point(0, 25);
        treeView1.Size = new Size(292, 248);
        treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Left | 
            AnchorStyles.Bottom | AnchorStyles.Right;
        treeView1.CheckBoxes = true;

        // Add nodes to treeView1.
        TreeNode node;
        for (int x = 0; x < 3; ++x)
        {
            // Add a root node.
            node = treeView1.Nodes.Add(String.Format("Node{0}", x*4));
            for (int y = 1; y < 4; ++y)
            {
                // Add a node as a child of the previously added node.
                node = node.Nodes.Add(String.Format("Node{0}", x*4 + y));
            }
        }

        // Set the checked state of one of the nodes to
        // demonstrate the showCheckedNodesButton button behavior.
        treeView1.Nodes[1].Nodes[0].Nodes[0].Checked = true;

        // Initialize showCheckedNodesButton.
        showCheckedNodesButton.Size = new Size(144, 24);
        showCheckedNodesButton.Text = "Show Checked Nodes";
        showCheckedNodesButton.Click += 
            new EventHandler(showCheckedNodesButton_Click);

        // Initialize the form.
        this.ClientSize = new Size(292, 273);
        this.Controls.AddRange(new Control[]
 
            { showCheckedNodesButton, treeView1 } );

        this.ResumeLayout(false);
    }

    [STAThreadAttribute()]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    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;
    }

}
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public
 Form
{
private:
   TreeView^ treeView1;
   Button^ showCheckedNodesButton;
   TreeViewCancelEventHandler^ checkForCheckedChildren;

public:
   Form1()
   {
      treeView1 = gcnew TreeView;
      showCheckedNodesButton = gcnew Button;
      checkForCheckedChildren = gcnew TreeViewCancelEventHandler( this,
 &Form1::CheckForCheckedChildrenHandler );
      this->SuspendLayout();
      
      // Initialize treeView1.
      treeView1->Location = Point(0,25);
      treeView1->Size = System::Drawing::Size( 292, 248 );
      treeView1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top |
 AnchorStyles::Left | AnchorStyles::Bottom | AnchorStyles::Right);
      treeView1->CheckBoxes = true;
      
      // Add nodes to treeView1.
      TreeNode^ node;
      for ( int x = 0; x < 3; ++x )
      {
         
         // Add a root node.
         node = treeView1->Nodes->Add( String::Format( "Node{0}",
 x * 4 ) );
         for ( int y = 1; y < 4; ++y )
         {
            
            // Add a node as a child of the previously added node.
            node = node->Nodes->Add( String::Format( "Node{0}", x
 * 4 + y ) );

         }
      }
      
      // Set the checked state of one of the nodes to
      // demonstrate the showCheckedNodesButton button behavior.
      treeView1->Nodes[ 1 ]->Nodes[ 0 ]->Nodes[ 0 ]->Checked = true;
      
      // Initialize showCheckedNodesButton.
      showCheckedNodesButton->Size = System::Drawing::Size( 144, 24 );
      showCheckedNodesButton->Text = "Show Checked Nodes";
      showCheckedNodesButton->Click += gcnew EventHandler( this,
 &Form1::showCheckedNodesButton_Click );
      
      // Initialize the form.
      this->ClientSize = System::Drawing::Size( 292, 273 );
      array<Control^>^temp0 = {showCheckedNodesButton,treeView1};
      this->Controls->AddRange( temp0 );
      this->ResumeLayout( 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;
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;

public class Form1 extends Form
{
    private TreeView treeView1;
    private Button showCheckedNodesButton;
    private TreeViewCancelEventHandler checkForCheckedChildren;

    public Form1()
    {
        treeView1 = new TreeView();
        showCheckedNodesButton = new Button();
        checkForCheckedChildren =
            new TreeViewCancelEventHandler(CheckForCheckedChildrenHandler);

        this.SuspendLayout();
        // Initialize treeView1.
        treeView1.set_Location(new Point(0, 25));
        treeView1.set_Size(new Size(292, 248));
        treeView1.set_Anchor(AnchorStyles.Top | AnchorStyles.Left
            | AnchorStyles.Bottom | AnchorStyles.Right);
        treeView1.set_CheckBoxes(true);
        // Add nodes to treeView1.
        TreeNode node;
        for (int x = 0; x < 3; ++x) {
            // Add a root node.
            node = treeView1.get_Nodes().Add(String.Format
                ("Node{0}", (Int32)(x * 4)));
            for (int y = 1; y < 4; ++y)
 {
                // Add a node as a child of the previously added node.
                node = node.get_Nodes().Add(String.Format
                    ("Node{0}", (Int32)(x * 4 + y)));
            }
        }
        // Set the checked state of one of the nodes to
        // demonstrate the showCheckedNodesButton button behavior.
        treeView1.get_Nodes().get_Item(1).get_Nodes().get_Item(0).
            get_Nodes().get_Item(0).set_Checked(true);
        // Initialize showCheckedNodesButton.
        showCheckedNodesButton.set_Size(new Size(144, 24));
        showCheckedNodesButton.set_Text("Show Checked Nodes");
        showCheckedNodesButton.add_Click(
            new EventHandler(showCheckedNodesButton_Click));
        // Initialize the form.
        this.set_ClientSize(new Size(292, 273));
        this.get_Controls().AddRange(new Control[]
            { showCheckedNodesButton, treeView1 });

        this.ResumeLayout(false);
    } //Form1

    public static void main(String[]
 args)
    {
        Application.Run(new Form1());
    } //main

    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
} //Form1
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

「TreeView.CheckBoxes プロパティ」の関連用語

TreeView.CheckBoxes プロパティのお隣キーワード
検索ランキング

   

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



TreeView.CheckBoxes プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS