TreeView 节点拖拽
程序员文章站
2022-05-03 13:53:52
1 public Form1() 2 { 3 InitializeComponent(); 4 treeView1.AllowDrop = true; 5 treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag); 6 tr... ......
1 public form1() 2 { 3 initializecomponent(); 4 treeview1.allowdrop = true; 5 treeview1.itemdrag += new itemdrageventhandler(treeview1_itemdrag); 6 treeview1.dragenter += new drageventhandler(treeview1_dragenter); 7 treeview1.dragdrop += new drageventhandler(treeview1_dragdrop); 8 } 9 void treeview1_dragdrop(object sender, drageventargs e) 10 { 11 treenode movenode = (treenode)e.data.getdata("system.windows.forms.treenode"); 12 //根据鼠标坐标确定要移动到的目标节点 13 point pt; 14 treenode targenode; 15 pt = ((treeview)(sender)).pointtoclient(new point(e.x, e.y)); 16 targenode = this.treeview1.getnodeat(pt); 17 //如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端 18 treenode newmovenode = (treenode)movenode.clone(); 19 if (targenode.nodes.count == 0) 20 { 21 targenode.parent.nodes.insert(targenode.index, newmovenode); 22 } 23 else 24 { 25 targenode.nodes.insert(targenode.nodes.count, newmovenode); 26 } 27 //更新当前拖动的节点选择 28 treeview1.selectednode = newmovenode; 29 //展开目标节点,便于显示拖放效果 30 targenode.expand(); 31 //移除拖放的节点 32 movenode.remove(); 33 } 34 void treeview1_dragenter(object sender, drageventargs e) 35 { 36 if (e.data.getdatapresent("system.windows.forms.treenode")) 37 { 38 e.effect = dragdropeffects.move; 39 } 40 else 41 { 42 e.effect = dragdropeffects.none; 43 } 44 } 45 void treeview1_itemdrag(object sender, itemdrageventargs e) 46 { 47 if (e.button == mousebuttons.left) 48 { 49 dodragdrop(e.item, dragdropeffects.move); 50 } 51 }
上一篇: C#封装的VSTO Excel操作类(20180903更新)
下一篇: c# 自定义日期的时分秒