如何:將捷徑功能表附加至 TreeView 節點
Windows Forms TreeView 控制項會顯示節點的階層,類似於在 [Windows 檔案總管] 左窗格中顯示的檔案和資料夾。 藉由設定 ContextMenuStrip 屬性,您就可以在使用者在 TreeView 控制項按右鍵時,將內容有區別的作業提供給使用者。 藉由將 ContextMenuStrip 元件與個別 TreeNode 項目產生關聯,您就可以將自訂的 [捷徑功能表] 功能層級新增至 TreeView 控制項。
以程式設計方式建立 [捷徑功能表] 與 TreeNode 的關聯
具現化 ContextMenuStrip 元件,然後針對您想要在執行階段提供的每個作業新增 ToolStripMenuItem。
將適當 ContextMenuStrip 的 TreeNode 屬性設定為您建立的 [捷徑功能表]。
若設定此屬性,當您在節點上按右鍵時,就會顯示 [捷徑功能表]。
下列程式碼範例會建立與 TreeView 的根 ContextMenuStrip 相關聯的基本 TreeNode 和 TreeView。 您必須將功能表選項自訂為符合您所開發的 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