假如全部是父节点,要在其中某个父节点右键添加子节点(假如父节点索引值为3),怎么获取鼠标点击的这个父节点的索引值呢?
这个索引值就是代码中添加子节点方括号里面那个值 this.treeview1.Nodes[索引值].Nodes.Add("...")C#

解决方案 »

  1.   


            private void treeView1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    this.treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
                    this.treeView1.DoDragDrop(this.treeView1.SelectedNode, DragDropEffects.All);
                }
            }        private void treeView1_DragEnter(object sender, DragEventArgs e)
            {
                if ((e.Data.GetDataPresent(typeof(TreeNode))))
                {
                    e.Effect = DragDropEffects.All;
                }
            }        private void treeView1_DragDrop(object sender, DragEventArgs e)
            {
    //因为这里的x,y坐标是全局的,所以要转换一下。
                Point newLocation = treeView1.PointToClient(new Point(e.X, e.Y));
                TreeNode targetNode = this.treeView1.GetNodeAt(newLocation);            TreeNode tn = e.Data.GetData(typeof(TreeNode)) as TreeNode;            if (targetNode == tn)
                    return;            tn.Parent.Nodes.Remove(tn);            targetNode.Nodes.Add(tn);
            }上面的代码是我回答另外一个帖子写的,实现鼠标拖拽treeview节点。
    根据鼠标点击的坐标获得当前节点:treeView1.GetNodeAt(e.Location);
    后面.Index自然就是索引了。但得到节点了估计索引对你也没啥用了。