TreeNodeCollection.Count 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取集合中 TreeNode 对象的总数。
public:
property int Count { int get(); };
[System.ComponentModel.Browsable(false)]
public int Count { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Count : int
Public ReadOnly Property Count As Integer
属性值
集合中的 TreeNode 对象的总数。
实现
- 属性
示例
下面的代码示例显示 中的 对象数TreeNode,将集合的内容复制到数组Object中,并显示控件中Label树节点TreeNodeCollection的列表。 此示例要求具有 TreeView 在其 中至少有一个 TreeNodeLabel 的 ,并且 上的 控件FormTreeNodeCollection。
void CopyTreeNodes()
{
// Get the collection of TreeNodes.
TreeNodeCollection^ myNodeCollection = myTreeView->Nodes;
int myCount = myNodeCollection->Count;
myLabel->Text = String::Concat( myLabel->Text, "Number of nodes in the collection : ", myCount );
myLabel->Text = String::Concat( myLabel->Text, "\n\nElements of the Array after Copying from the collection :\n" );
// Create an Object array.
array<Object^>^myArray = gcnew array<Object^>(myCount);
// Copy the collection into an array.
myNodeCollection->CopyTo( myArray, 0 );
for ( int i = 0; i < myArray->Length; i++ )
{
myLabel->Text = myLabel->Text + (dynamic_cast<TreeNode^>(myArray[ i ]))->Text + "\n";
}
}
private void CopyTreeNodes()
{
// Get the collection of TreeNodes.
TreeNodeCollection myNodeCollection = myTreeView.Nodes;
int myCount = myNodeCollection.Count;
myLabel.Text += "Number of nodes in the collection :" + myCount;
myLabel.Text += "\n\nElements of the Array after Copying from the collection :\n";
// Create an Object array.
Object[] myArray = new Object[myCount];
// Copy the collection into an array.
myNodeCollection.CopyTo(myArray,0);
for(int i=0; i<myArray.Length; i++)
{
myLabel.Text += ((TreeNode)myArray[i]).Text + "\n";
}
}
Private Sub CopyTreeNodes()
' Get the collection of TreeNodes.
Dim myNodeCollection As TreeNodeCollection = myTreeView.Nodes
Dim myCount As Integer = myNodeCollection.Count
myLabel.Text += "Number of nodes in the collection :" + myCount.ToString()
myLabel.Text += ControlChars.NewLine + ControlChars.NewLine + _
"Elements of the Array after Copying from the collection :" + ControlChars.NewLine
' Create an Object array.
Dim myArray(myCount -1) As Object
' Copy the collection into an array.
myNodeCollection.CopyTo(myArray, 0)
Dim i As Integer
For i = 0 To myArray.Length - 1
myLabel.Text += CType(myArray(i), TreeNode).Text + ControlChars.NewLine
Next i
End Sub
注解
属性 Count 包含分配给集合的对象数 TreeNode 。 可以使用 Count 属性值作为循环的上限来循环访问集合。
注意
由于集合的索引值是从零开始的索引,因此必须从循环变量中减去一个索引。 如果不考虑这一点,将超出集合的上限并引发 IndexOutOfRangeException 异常。