问题一:怎样为“Treeview”的Node节点绑定onmousedown事件???
是不是为“TreeView”绑定“onmousedown”,有关“Node”的事件只有“NodeMouseClick”、“NodeMouseDoubleClick”、“NodeMouseHover”???问题二:怎样获取点击的是“鼠标右键”还是“左键”???

解决方案 »

  1.   

    在WinForm程序中添加了一个TreeView,但是当右击一个节点没有选择删除的时候,再次右击TreeView空白处,删除了刚才右击的节点的处理方法:1.为该TreeView添加MouseDown事件,接下来写如下代码:C-sharp代码 
    1.TreeNode tn = new TreeNode();   
    2.private void treeView1_MouseDown(object sender, MouseEventArgs e)   
    3.{   
    4.    Point p = new Point();   
    5.    p.X = e.X;   
    6.    p.Y = e.Y;   
    7.    tn = this.treeView1.GetNodeAt(p);   
    8.}  
            TreeNode tn = new TreeNode();
            private void treeView1_MouseDown(object sender, MouseEventArgs e)
            {
                Point p = new Point();
                p.X = e.X;
                p.Y = e.Y;
                tn = this.treeView1.GetNodeAt(p);
            }这段代码里面的GetNodeAt(P)方法是检索位于指定点(以坐标表示)的树节点;如果有节点则会得到该节点,否则返回Null;
      

  2.   

    private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                     MessageBox.Show("你按下鼠标左键", "提示");
                }
                else
                {
                     MessageBox.Show("你按下鼠标右键","提示");
                }
            } 
      

  3.   

    看MSDNpublic partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                //设置属性
                treeView1.AllowDrop = true;
                treeView1.Dock = DockStyle.Fill;
                
                this.treeView1.Nodes.AddRange(new TreeNode[] {
                new TreeNode("IT技术", new TreeNode[] {
                new TreeNode(".Net", new TreeNode[] {
                new TreeNode("Silverlight"), new TreeNode("ASP.NET"),new TreeNode("Winform")}),
                new TreeNode("Java技术", new TreeNode[] {new TreeNode("Java EE"),
                new TreeNode("Java SE"), new TreeNode("Java ME")}),
                new TreeNode("其它技术", new TreeNode[] {
                new TreeNode("Web开发", new TreeNode[] {new TreeNode("ASP"),
                new TreeNode("PHP", new TreeNode[] {new TreeNode("AJAX"),new TreeNode("JS")})}),
                new TreeNode("数据库"),new TreeNode("移动设备")})})});            treeView1.ExpandAll();        }        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                // Move the dragged node when the left mouse button is used.
                if (e.Button == MouseButtons.Left)
                {
                    DoDragDrop(e.Item, DragDropEffects.Move);
                }            // Copy the dragged node when the right mouse button is used.
                else if (e.Button == MouseButtons.Right)
                {
                    DoDragDrop(e.Item, DragDropEffects.Copy);
                }
            }        private void treeView1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = e.AllowedEffect;
            }        private void treeView1_DragOver(object sender, DragEventArgs e)
            {
                // Retrieve the client coordinates of the mouse position.
                Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));            // Select the node at the mouse position.
                treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);
            }        private void treeView1_DragDrop(object sender, DragEventArgs e)
            {
                // Retrieve the client coordinates of the drop location.
                Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));            // Retrieve the node at the drop location.
                TreeNode targetNode = treeView1.GetNodeAt(targetPoint);            // Retrieve the node that was dragged.
                TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));            // Confirm that the node at the drop location is not 
                // the dragged node or a descendant of the dragged node.
                if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
                {
                    // If it is a move operation, remove the node from its current 
                    // location and add it to the node at the drop location.
                    if (e.Effect == DragDropEffects.Move)
                    {
                        draggedNode.Remove();
                        targetNode.Nodes.Add(draggedNode);
                    }                // If it is a copy operation, clone the dragged node 
                    // and add it to the node at the drop location.
                    else if (e.Effect == DragDropEffects.Copy)
                    {
                        targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
                    }                // Expand the node at the location 
                    // to show the dropped node.
                    targetNode.Expand();
                }
            }
            // Determine whether one node is a parent 
            // or ancestor of a second node.
            private bool ContainsNode(TreeNode node1, TreeNode node2)
            {
                // Check the parent node of the second node.
                if (node2.Parent == null) return false;
                if (node2.Parent.Equals(node1)) return true;            // If the parent node is not null or equal to the first node, 
                // call the ContainsNode method recursively using the parent of 
                // the second node.
                return ContainsNode(node1, node2.Parent);
            }    }