次の方法で共有


方法: TreeView コントロールまたは ListView コントロールにカスタム情報を追加する (Windows フォーム)

Windows フォーム TreeView コントロールに派生ノードを作成することも、ListView コントロールの派生項目を作成することもできます。 派生を使用すると、必要なフィールドと、それらを処理するためのカスタム メソッドとコンストラクターを追加できます。 この機能の 1 つの用途は、各ツリー ノードまたはリスト アイテムに Customer オブジェクトをアタッチすることです。 ここでは、TreeView コントロールの例を示しますが、ListView コントロールにも同じ方法を使用できます。

ツリー ノードを生成するには

  • TreeNode クラスから派生した新しいノード クラスを作成します。このクラスには、ファイル パスを記録するユーザー設定フィールドがあります。

    Class myTreeNode
       Inherits TreeNode
    
       Public FilePath As String
    
       Sub New(ByVal fp As String)
          MyBase.New()
          FilePath = fp
          Me.Text = fp.Substring(fp.LastIndexOf("\"))
       End Sub
    End Class
    
    class myTreeNode : TreeNode
    {
       public string FilePath;
    
       public myTreeNode(string fp)
       {
          FilePath = fp;
          this.Text = fp.Substring(fp.LastIndexOf("\\"));
       }
    }
    
    ref class myTreeNode : public TreeNode
    {
    public:
       System::String ^ FilePath;
    
       myTreeNode(System::String ^ fp)
       {
          FilePath = fp;
          this->Text = fp->Substring(fp->LastIndexOf("\\"));
       }
    };
    

派生されたツリー ノードを使用するには

  1. 新しい派生ツリー ノードを関数呼び出しのパラメーターとして使用できます。

    次の例では、テキスト ファイルの場所に設定されているパスが [マイ ドキュメント] フォルダーです。 これは、Windows オペレーティング システムを実行しているほとんどのコンピューターにこのディレクトリが含まれると想定できるためです。 これにより、システム アクセス レベルが最小限のユーザーでも、アプリケーションを安全に実行できます。

    ' You should replace the bold text file
    ' in the sample below with a text file of your own choosing.
    TreeView1.Nodes.Add(New myTreeNode (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\ TextFile.txt ") )
    
    // You should replace the bold text file
    // in the sample below with a text file of your own choosing.
    // Note the escape character used (@) when specifying the path.
    treeView1.Nodes.Add(new myTreeNode(System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\TextFile.txt") );
    
    // You should replace the bold text file
    // in the sample below with a text file of your own choosing.
    treeView1->Nodes->Add(new myTreeNode(String::Concat(
       System::Environment::GetFolderPath
       (System::Environment::SpecialFolder::Personal),
       "\\TextFile.txt")));
    
  2. ツリー ノードが渡され、TreeNode クラスとして型指定されている場合は、派生クラスにキャストし直す必要があります。 キャストとは、ある型のオブジェクトから別の型のオブジェクトに明示的に変換することです。 キャストの詳細については、「暗黙の型変換と明示的な型変換 (Visual Basic)」、「キャストと型変換」(Visual C#)、または「キャスト演算子: ()」(Visual C++) を参照してください。

    Public Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
       Dim mynode As myTreeNode
       mynode = CType(e.node, myTreeNode)
       MessageBox.Show("Node selected is " & mynode.filepath)
    End Sub
    
    protected void treeView1_AfterSelect (object sender,
    System.Windows.Forms.TreeViewEventArgs e)
    {
       myTreeNode myNode = (myTreeNode)e.Node;
       MessageBox.Show("Node selected is " + myNode.FilePath);
    }
    
    private:
       System::Void treeView1_AfterSelect(System::Object ^  sender,
          System::Windows::Forms::TreeViewEventArgs ^  e)
       {
          myTreeNode ^ myNode = safe_cast<myTreeNode^>(e->Node);
          MessageBox::Show(String::Concat("Node selected is ",
             myNode->FilePath));
       }
    

こちらも参照ください