2012-12-23 66 views
2

我已經有了一個帶有幾個treenodes的treeview。當我右鍵單擊一個treenode時,我會得到一個帶有不同選項的contextmenu ,例如'刪除項目'。從contextmenu-item eventHandler獲取treenode

是否有一種簡單的方法可以在contextmenu-item的eventHandler中獲取右鍵單擊的treenode對象?

回答

0

如果(右)點擊一個節點,是不是成爲選擇的節點?

TreeNode needed = TreeViewX.SelectedNode; 

乾杯

+2

這個答案不適用於所有情況。如果右鍵單擊未選中的節點,它將顯示爲選定節點。當菜單條狀項目的事件處理程序觸發時,TreeView將已將SelectedNode重置爲之前的狀態。 @ erem's是一個更準確的解決方案。 – Kleinux

6

我前一段時間也有類似的問題,我想出了這樣的

解決方案創建自己的myContextMenuStrip類中你覆蓋了OnItemClicked方法,它從標準的ContextMenuStrip

public class myContextMenuStrip : ContextMenuStrip 
    { 
     public TreeNode tn; 

     public myContextMenuStrip() { } 

     protected override void OnItemClicked(ToolStripItemClickedEventArgs e) 
     { 
      base.OnItemClicked(e); 
      if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text); 
     } 
    } 

派生單擊特定menuItem時顯示MessageBox。

所以,當你用鼠標右鍵點擊treeView項目時,它將從鼠標指針下面檢索節點並將它傳遞給你的myContextMenuStrip。

private void treeView1_MouseClick(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      TreeNode tn = treeView1.GetNodeAt(e.Location); 
      myMenu.tn = tn; 
     } 
    } 

並在formLoad上初始化myContextMenuStrip,添加項目並將其綁定到treeView。

private void Form1_Load(object sender, EventArgs e) 
    { 
     myMenu = new myContextMenuStrip(); 
     myMenu.Items.Add("asd"); 
     treeView1.ContextMenuStrip = myMenu; 
    } 

我知道這是不是很優雅的方式,但它的簡單,它的工作原理(與通過內部的EventArgs treeNode的價值理念這可能是難以實現的,甚至是不可能的 - 沒有嘗試自己,很少有嘗試但辭職)。

整個工作代碼,TreeView控件上的形式需要:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public myContextMenuStrip myMenu; 

    private void treeView1_MouseClick(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      TreeNode tn = treeView1.GetNodeAt(e.Location); 
      myMenu.tn = tn; 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     myMenu = new myContextMenuStrip(); 
     myMenu.Items.Add("asd"); 
     treeView1.ContextMenuStrip = myMenu; 
    } 

    public class myContextMenuStrip : ContextMenuStrip 
    { 
     public TreeNode tn; 

     public myContextMenuStrip() { } 

     protected override void OnItemClicked(ToolStripItemClickedEventArgs e) 
     { 
      base.OnItemClicked(e); 
      if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text); 
     } 
    } 
} 
} 
+1

Thnx。這可能工作,但Valentijn的解決方案更明顯:) –

+0

嗯,是的,我過分複雜:) – erem

0

另一個想法是上下文菜單的標籤屬性設置爲節點對象,然後從事件處理程序只是訪問它。當然,這隻有在你不使用標籤的情況下才有效。

private void MyTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
      MyContextMenu.Tag = e.Node; 
      MyContextMenu.Show(this, e.Location); 
     } 
    } 
private void MyToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     //Get TreeNode from Tag 
     //Note: Could also get ContextMenu from sender, 
     //but we already have it, so just access it directly 
     TreeNode node = MyContextMenu.Tag as TreeNode; 
     if (node == null) 
      return; 
     //Do stuff with node here 
    }