TreeView.CheckBoxes 속성
tree view 컨트롤의 트리 노드 옆에 확인란이 표시되는지 여부를 나타내는 값을 가져오거나 설정합니다.
네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)
구문
‘선언
Public Property CheckBoxes As Boolean
‘사용 방법
Dim instance As TreeView
Dim value As Boolean
value = instance.CheckBoxes
instance.CheckBoxes = value
public bool CheckBoxes { get; set; }
public:
property bool CheckBoxes {
bool get ();
void set (bool value);
}
/** @property */
public boolean get_CheckBoxes ()
/** @property */
public void set_CheckBoxes (boolean value)
public function get CheckBoxes () : boolean
public function set CheckBoxes (value : boolean)
속성 값
tree view 컨트롤의 각 트리 노드 옆에 확인란이 표시되면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.
설명
트리 노드 레이블 및 트리 노드 Image(있는 경우) 왼쪽에 확인란이 표시됩니다. 확인란을 사용하면 한 번에 둘 이상의 트리 노드를 선택할 수 있습니다.
TreeView의 CheckBoxes 속성을 true로 설정하고 StateImageList 속성을 설정하면 TreeView에 포함된 TreeNode는 StateImageList의 첫 번째 및 두 번째 이미지를 표시하여 각각 선택되지 않은 상태와 선택된 상태를 나타냅니다. 그러나 다음과 같은 경우가 발생하면 표시되는 이미지가 달라질 수 있습니다. 노드의 SelectedImageIndex가 0 또는 1 이외의 값으로 설정되고 부모 TreeView의 CheckBoxes 속성이 false로 설정된 경우 SelectedImageIndex는 설정되지 않았음을 나타내는 -1로 다시 설정되지 않습니다. 이 경우 가리키는 인덱스 위치의 상태 이미지가 표시됩니다. CheckBoxes를 다시 true로 설정하면 StateImageList의 첫 번째 및 두 번째 이미지가 표시되어 각각 선택된 상태와 선택되지 않은 상태를 나타냅니다.
참고
런타임에 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
플랫폼
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원
참고 항목
참조
TreeView 클래스
TreeView 멤버
System.Windows.Forms 네임스페이스
TreeNode.Checked 속성