次の方法で共有


方法: ショートカット メニューを TreeView ノードにアタッチする

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

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

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

  2. ContextMenuStrip コンポーネントをインスタンス化し、実行時に使用可能にする操作ごとに ToolStripMenuItem を追加します。

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

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

次のコード例では、TreeViewのルート ContextMenuStrip に関連付けられた基本的な TreeNodeTreeView を作成します。 メニューの選択肢は、開発中の 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


こちらも参照ください