本帖最后由 ffff666666 于 2018-11-30 17:49 编辑
- namespace DragAndDrop
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- c1TreeView1.AllowDrop = true;
- c1TreeView2.AllowDrop = true;
- c1TreeView1.EnableDragDrop = true;
- c1TreeView2.EnableDragDrop = true;
- c1TreeView1.ItemDrag += C1TreeView_ItemDrag;
- c1TreeView2.ItemDrag += C1TreeView_ItemDrag;
- c1TreeView1.DragEnter += C1TreeView_DragEnter;
- c1TreeView2.DragEnter += C1TreeView_DragEnter;
- c1TreeView1.DragDrop += C1TreeView_DragDrop;
- c1TreeView2.DragDrop += C1TreeView_DragDrop;
- }
- private C1TreeNode oldTN;
- private void C1TreeView_DragDrop(object sender, DragEventArgs e)
- {
- //第三步
- var treeView = (C1TreeView)sender;
- // get dropped data
- var distanceNodes = (C1TreeNode[])e.Data.GetData(typeof(C1TreeNode[]));
- var distanceNode = distanceNodes[0];
- var pos = (DragDropPosition)e.Data.GetData(typeof(DragDropPosition));
- if (pos == DragDropPosition.Inner)
- {
-
- distanceNode.ParentCollection.Remove(distanceNode);
- treeView.Nodes.Add(oldTN);
- MessageBox.Show("no");
- return;
- }
- }
- private void C1TreeView_DragEnter(object sender, DragEventArgs e)
- {
- //第二步
- e.Effect = DragDropEffects.Move;
- //获取拖动之前的节点
- var distanceNodes = (C1TreeNode[])e.Data.GetData(typeof(C1TreeNode[]));
- oldTN = distanceNodes[0];
- }
- private void C1TreeView_ItemDrag(object sender, C1.Win.TreeView.C1TreeViewItemDragEventArgs e)
- {
- //第一步
- DoDragDrop(e.DraggedData, DragDropEffects.Move);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- for (int i = 0; i < 5; i++)
- {
- c1TreeView1.Nodes.Add("Node" + i);
- c1TreeView2.Nodes.Add("Node" + i * 100);
- for (int j = 0; j < 5; j++)
- {
- c1TreeView1.Nodes[i].Nodes.Add("Node" + (i * 10 + j));
- c1TreeView2.Nodes[i].Nodes.Add("Node" + (i * 100 + j));
- }
- }
- }
- }
- }
复制代码
如上面的代码,借用treeview的DragAndDrop的示例;如果是这样inner拖动的,就需要把移动过去的删掉,然后再在tree后面新增一个新的,通过这种方式来禁止往子节点的拖动,有没有简单的方法呢? |