如何:将快捷菜单附加到 TreeView 节点

更新:2007 年 11 月

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


// 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:
    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);
    }

请参见

参考

ContextMenuStrip

其他资源

TreeView 控件(Windows 窗体)