方法: ショートカット メニューを TreeView ノードに追加する

Windows フォームの TreeView コントロールでは、Windows エクスプローラーの左側のウィンドウに表示されるファイルやフォルダーと同じように、ノードの階層を表示することができます。 ContextMenuStrip プロパティを設定することで、ユーザーが TreeView コントロールを右クリックしたときに、状況に応じた操作を提供できます。 ContextMenuStrip コンポーネントを TreeNode の個々の項目に関連付けることで、カスタマイズされたレベルのショートカット メニュー機能を TreeView コントロールに追加できます。

プログラムを使用してショートカット メニューを TreeNode に関連付けるには

  1. 適切なプロパティ設定を使用して TreeView コントロールをインスタンス化し、ルート TreeNode を作成して、サブノードを追加します。

  2. ContextMenuStrip コンポーネントをインスタンス化し、実行時に使用できるようにする各操作の ToolStripMenuItem を追加します。

  3. 適切な TreeNodeContextMenuStrip プロパティを、作成するショートカット メニューに設定します。

  4. このプロパティを設定すると、ユーザーがノードを右クリックしたときにショートカット メニューが表示されます。

次のコード例では、TreeView のルート TreeNode に関連付けられた基本の TreeViewContextMenuStrip を作成します。 メニューの選択肢は、開発中の TreeView に適したものにカスタマイズする必要があります。 また、これらのメニュー項目の Click イベントを処理するコードを記述する必要があります。

    // Declare the TreeView and ContextMenuStrip
private:
    TreeView^ menuTreeView;
private:
    System::Windows::Forms::ContextMenuStrip^ docMenu;

public:
    void InitializeMenuTreeView()
    {
        // Create the TreeView.
        menuTreeView = gcnew TreeView();
        menuTreeView->Size = System::Drawing::Size(200, 200);

        // Create the root node.
        TreeNode^ docNode = gcnew TreeNode("Documents");

        // Add some additional nodes.
        docNode->Nodes->Add("phoneList.doc");
        docNode->Nodes->Add("resume.doc");

        // Add the root nodes to the TreeView.
        menuTreeView->Nodes->Add(docNode);

        // Create the ContextMenuStrip.
        docMenu = gcnew System::Windows::Forms::ContextMenuStrip();

        //Create some menu items.
        ToolStripMenuItem^ openLabel = gcnew ToolStripMenuItem();
        openLabel->Text = "Open";
        ToolStripMenuItem^ deleteLabel = gcnew ToolStripMenuItem();
        deleteLabel->Text = "Delete";
        ToolStripMenuItem^ renameLabel = gcnew ToolStripMenuItem();
        renameLabel->Text = "Rename";

        //Add the menu items to the menu.
        docMenu->Items->AddRange(gcnew array<ToolStripMenuItem^>{openLabel,
            deleteLabel, renameLabel});

        // Set the ContextMenuStrip property to the ContextMenuStrip.
        docNode->ContextMenuStrip = docMenu;

        // Add the TreeView to the form.
        this->Controls->Add(menuTreeView);
    }
// Declare the TreeView and ContextMenuStrip
private TreeView menuTreeView;
private ContextMenuStrip docMenu;

public void InitializeMenuTreeView()
{
    // Create the TreeView.
    menuTreeView = new TreeView();
    menuTreeView.Size = new Size(200, 200);

    // Create the root node.
    TreeNode docNode = new TreeNode("Documents");

    // Add some additional nodes.
    docNode.Nodes.Add("phoneList.doc");
    docNode.Nodes.Add("resume.doc");

    // Add the root nodes to the TreeView.
    menuTreeView.Nodes.Add(docNode);

    // Create the ContextMenuStrip.
    docMenu = new ContextMenuStrip();

    //Create some menu items.
    ToolStripMenuItem openLabel = new ToolStripMenuItem();
    openLabel.Text = "Open";
    ToolStripMenuItem deleteLabel = new ToolStripMenuItem();
    deleteLabel.Text = "Delete";
    ToolStripMenuItem renameLabel = new ToolStripMenuItem();
    renameLabel.Text = "Rename";

    //Add the menu items to the menu.
    docMenu.Items.AddRange(new ToolStripMenuItem[]{openLabel,
        deleteLabel, renameLabel});

    // Set the ContextMenuStrip property to the ContextMenuStrip.
    docNode.ContextMenuStrip = docMenu;

    // Add the TreeView to the form.
    this.Controls.Add(menuTreeView);
}

' Declare the TreeView and ContextMenuStrip
Private menuTreeView As TreeView
Private docMenu As ContextMenuStrip


Public Sub InitializeMenuTreeView() 

    ' Create the TreeView.
    menuTreeView = New TreeView()
    menuTreeView.Size = New Size(200, 200)
    
    ' Create the root node.
    Dim docNode As New TreeNode("Documents")
    
    ' Add some additional nodes.
    docNode.Nodes.Add("phoneList.doc")
    docNode.Nodes.Add("resume.doc")
    
    ' Add the root nodes to the TreeView.
    menuTreeView.Nodes.Add(docNode)
    
    ' Create the ContextMenuStrip.
    docMenu = New ContextMenuStrip()
    
    'Create some menu items.
    Dim openLabel As New ToolStripMenuItem()
    openLabel.Text = "Open"
    Dim deleteLabel As New ToolStripMenuItem()
    deleteLabel.Text = "Delete"
    Dim renameLabel As New ToolStripMenuItem()
    renameLabel.Text = "Rename"
    
    'Add the menu items to the menu.
    docMenu.Items.AddRange(New ToolStripMenuItem() _
        {openLabel, deleteLabel, renameLabel})
    
    ' Set the ContextMenuStrip property to the ContextMenuStrip.
    docNode.ContextMenuStrip = docMenu
    
    ' Add the TreeView to the form.
    Me.Controls.Add(menuTreeView)

End Sub


関連項目