This topic has not yet been rated - Rate this topic

TreeNode.NodeFont Property

Gets or sets the font used to display the text on the tree node's label.

[Visual Basic]
Public Property NodeFont As Font
[C#]
public Font NodeFont {get; set;}
[C++]
public: __property Font* get_NodeFont();
public: __property void set_NodeFont(Font*);
[JScript]
public function get NodeFont() : Font;
public function set NodeFont(Font);

Property Value

The Font used to display the text on the tree node's label.

Remarks

If a null reference (Nothing in Visual Basic), the Font used is the Font property value of the TreeView control that this node is attached to.

Note   If the node font is larger than the Font property value set in the TreeView control, the tree node label text is clipped.

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

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace | Font | ForeColor

Did you find this helpful?
(1500 characters remaining)