Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TreeView.ItemHeight Property

Gets or sets the height of each tree node in the tree view control.

[Visual Basic]
Public Property ItemHeight As Integer
[C#]
public int ItemHeight {get; set;}
[C++]
public: __property int get_ItemHeight();
public: __property void set_ItemHeight(int);
[JScript]
public function get ItemHeight() : int;
public function set ItemHeight(int);

Property Value

The height, in pixels, of each tree node in the tree view.

Exceptions

Exception Type Condition
ArgumentException The assigned value is less than one.

-or-

The assigned value is greater than the MaxValue value.

Example

[Visual Basic, C#, C++] The following example changes the size NodeFont to the specified size and adjusts the ItemHeight of the tree node's parent TreeView contorl. This example assumes you have a Form with a TreeView control with a collection of TreeNode objects, and a ComboBox containing font sizes.

[Visual Basic] 
Private Sub Button1_Click(sender As Object, e As EventArgs)
   myTreeView.ItemHeight = 5
   myTreeView.SelectedNode.NodeFont = New Font("Arial", 5)

   ' Get the font size from combobox.
   Dim selectedString As String = myComboBox.SelectedItem.ToString()
   Dim myNodeFontSize As Integer = Int32.Parse(selectedString)

   ' Set the font of root node.
   myTreeView.SelectedNode.NodeFont = New Font("Arial", myNodeFontSize)
   Dim i As Integer
   For  i = 0 To (myTreeView.Nodes(0).Nodes.Count) - 1
      ' Set the font of child nodes.
      myTreeView.Nodes(0).Nodes(i).NodeFont = New Font("Arial", _
        myNodeFontSize)
   Next i

   ' Get the bounds of the tree node.
   Dim myRectangle As Rectangle = myTreeView.SelectedNode.Bounds
   Dim myNodeHeight As Integer = myRectangle.Height
   If myNodeHeight < myNodeFontSize Then
      myNodeHeight = myNodeFontSize
   End If
   myTreeView.ItemHeight = myNodeHeight + 4
End Sub 

[C#] 
private void Button1_Click(object sender,EventArgs e)
{
   myTreeView.ItemHeight = 5;
   myTreeView.SelectedNode.NodeFont = new Font("Arial",5);

   // Get the font size from combobox.
   string selectedString = myComboBox.SelectedItem.ToString();
   int myNodeFontSize = Int32.Parse(selectedString);

   // Set the font of root node.
   myTreeView.SelectedNode.NodeFont = new Font("Arial",myNodeFontSize);
   for(int i = 0; i < myTreeView.Nodes[0].Nodes.Count; i++)
   {
      // Set the font of child nodes.
      myTreeView.Nodes[0].Nodes[i].NodeFont =
        new Font("Arial",myNodeFontSize);
   }

   // Get the bounds of the tree node.
   Rectangle myRectangle = myTreeView.SelectedNode.Bounds;
   int myNodeHeight = myRectangle.Height;
   if(myNodeHeight < myNodeFontSize)
   {
      myNodeHeight = myNodeFontSize;
   }
   myTreeView.ItemHeight = myNodeHeight + 4;
}

[C++] 
private:
    void Button1_Click(Object* /*sender*/, EventArgs* /*e*/) {
        myTreeView->ItemHeight = 5;
        myTreeView->SelectedNode->NodeFont = new System::Drawing::Font(S"Arial", 5);

        // Get the font size from combobox.
        String* selectedString = myComboBox->SelectedItem->ToString();
        int myNodeFontSize = Int32::Parse(selectedString);

        // Set the font of root node.
        myTreeView->SelectedNode->NodeFont = new System::Drawing::Font(S"Arial", 
            (float)myNodeFontSize);
        for (int i = 0; i < myTreeView->Nodes->Item[0]->Nodes->Count; i++) {
            // Set the font of child nodes.
            myTreeView->Nodes->Item[0]->Nodes->Item[i]->NodeFont = 
                new System::Drawing::Font(S"Arial", (float)myNodeFontSize);
        }

        // Get the bounds of the tree node.
        Rectangle myRectangle = myTreeView->SelectedNode->Bounds;
        int myNodeHeight = myRectangle.Height;
        if (myNodeHeight < myNodeFontSize) {
            myNodeHeight = myNodeFontSize;
        }
        myTreeView->ItemHeight = myNodeHeight + 4;
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TreeView Class | TreeView Members | System.Windows.Forms Namespace | MaxValue

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.