次の方法で共有


TreeNode.NodeFont プロパティ

ツリー ノードのラベル テキストを表示するために使用するフォントを取得または設定します。

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

プロパティ値

ツリー ノードのラベル テキストを表示するために使用する Font

解説

null 参照 (Visual Basic では Nothing) の場合、使用される Font は、対象のノードが割り当てられている TreeView コントロールの Font プロパティ値になります。

メモ   ノードのフォントが TreeView コントロールの Font プロパティで設定されている値よりも大きい場合、ツリー ノード ラベルのテキストはクリップされます。

使用例

[Visual Basic, C#, C++] NodeFont のサイズを指定されたサイズに変更し、ツリー ノードの親 TreeView コントロールの ItemHeight を調整する例を次に示します。この例は、 TreeNode オブジェクトのコレクションが設定された TreeView コントロールと、フォント サイズの一覧が格納されている ComboBox が配置された Form があることを前提にしています。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TreeNode クラス | TreeNode メンバ | System.Windows.Forms 名前空間 | Font | ForeColor