国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#實現(xiàn)listview中節(jié)點的拖拉

2024-07-21 02:26:18
字體:
來源:轉載
供稿:網友

using system;
using system.drawing;
using system.windows.forms;

public class form4 : form
{
 private treeview treeview1;

 public form4()
 {
  treeview1 = new treeview();

  this.suspendlayout();

  // initialize treeview1.
  treeview1.allowdrop = true;
  treeview1.dock = dockstyle.fill;

  // add nodes to treeview1.
  treenode node;
  for (int x = 0; x < 3; ++x)
  {
   // add a root node to treeview1.
   node = treeview1.nodes.add(string.format("node{0}", x*4));
   for (int y = 1; y < 4; ++y)
   {
    // add a child node to the previously added node.
    node = node.nodes.add(string.format("node{0}", x*4 + y));
   }
  }

  // add event handlers for the required drag events.
  treeview1.itemdrag += new itemdrageventhandler(treeview1_itemdrag);
  treeview1.dragenter += new drageventhandler(treeview1_dragenter);
  treeview1.dragover += new drageventhandler(treeview1_dragover);
  treeview1.dragdrop += new drageventhandler(treeview1_dragdrop);

  // initialize the form.
  this.clientsize = new size(292, 273);
  this.controls.add(treeview1);

  this.resumelayout(false);
 }

 [stathread]
// static void main()
// {
//  application.run(new form1());
// }
//
 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);
  }
 }

 // set the target drop effect to the effect
 // specified in the itemdrag event handler.
 private void treeview1_dragenter(object sender, drageventargs e)
 {
  e.effect = e.allowedeffect;
 }

 // select the node under the mouse pointer to indicate the
 // expected drop location.
 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);
 }

}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 瓦房店市| 新蔡县| 日喀则市| 林甸县| 西贡区| 高邑县| 普宁市| 辽源市| 叙永县| 马边| 宜城市| 平利县| 岢岚县| 河东区| 日照市| 雷山县| 黎川县| 莫力| 张掖市| 泽普县| 拉萨市| 江达县| 临漳县| 桃源县| 嘉义县| 建德市| 南靖县| 旬邑县| 瓮安县| 元阳县| 汉源县| 蓬溪县| 平昌县| 晋江市| 平陆县| 资阳市| 车致| 沛县| 宜都市| 天长市| 宾川县|