Udostępnij za pośrednictwem


Porady: dołączanie menu ShortCut do węzła TreeView

Kontrolka Formularze systemu TreeView Windows wyświetla hierarchię węzłów, podobnie jak pliki i foldery wyświetlane w okienku po lewej stronie Eksploratora Windows. Ustawiając właściwość, możesz podać użytkownikowi ContextMenuStrip operacje kontekstowe po kliknięciu kontrolki prawym przyciskiem TreeView myszy. Kojarząc ContextMenuStrip składnik z poszczególnymi TreeNode elementami, można dodać do TreeView kontrolek dostosowany poziom funkcjonalności menu skrótów.

Aby programowo skojarzyć menu skrótów z węzłem TreeNode

  1. Utwórz wystąpienie TreeView kontrolki z odpowiednimi ustawieniami właściwości, utwórz katalog główny TreeNode, a następnie dodaj podwęźle.

  2. ContextMenuStrip Utwórz wystąpienie składnika, a następnie dodaj element ToolStripMenuItem dla każdej operacji, którą chcesz udostępnić w czasie wykonywania.

  3. ContextMenuStrip Ustaw właściwość odpowiedniego TreeNode do utworzonego menu skrótów.

  4. Po ustawieniu tej właściwości menu skrótów będzie wyświetlane po kliknięciu węzła prawym przyciskiem myszy.

Poniższy przykład kodu tworzy podstawowy TreeView i ContextMenuStrip skojarzony z katalogiem głównym TreeNode obiektu TreeView. Musisz dostosować opcje menu do tych, które pasują do opracowywanych elementów TreeView . Ponadto należy napisać kod do obsługi Click zdarzeń dla tych elementów menu.

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


Zobacz też