HOW TO:將捷徑功能表附加至 TreeView 節點
Windows Form TreeView 控制項顯示節點的階層架構,顯示的方法如同 Windows 檔案總管左窗格中所顯示的檔案和資料夾。 設定好 ContextMenuStrip 屬性之後,當使用者在 TreeView 控制項上按一下滑鼠右鍵時,便可提供內容相關性的作業。 將 ContextMenuStrip 元件與個別 TreeNode 項目產生關聯之後,便可將捷徑功能表功能的自訂層級加入至 TreeView 控制項。
若要以程式設計方式將捷徑功能表與 TreeNode 產生關聯
以適當的屬性設定產生 (Instantiate) TreeView 控制項,接著建立根 TreeNode,然後再加入子節點。
產生 ContextMenuStrip 元件,然後再為每個想在執行階段使用的作業加入 ToolStripMenuItem。
將適當的 TreeNode 的 ContextMenuStrip 屬性設為所建立的捷徑功能表。
設定好這個屬性之後,當在節點上按一下滑鼠右鍵時將會出現捷徑功能表。
下列程式碼範例將建立基本的 TreeView 以及與 TreeView 的根 TreeNode 關聯的 ContextMenuStrip。 您將需要自訂功能表選擇,以符合正在開發的 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);
}