Csharp: TreeList Drag and Drop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | /// <summary> /// https://www.codeproject.com/articles/3225/treelistview /// https://www.codeproject.com/Tips/572728/TreeListViewEx-Tree-List-and-Drag-and-Drop /// https://sourceforge.net/projects/objectlistview/ /// 涂聚文 20190704 /// Geovin Du /// </summary> public partial class TryTreeListView : Form { List< int > selectindex = new List< int >(); // TreeListViewItem draggedNode= new TreeListViewItem(); /// <summary> /// /// </summary> public string draggtext { set ; get ; } /// <summary> /// /// </summary> string beforeEdit = "" ; /// <summary> /// /// </summary> string beforeEditName = "" ; /// <summary> /// /// </summary> string afterEdit = "" ; string nodechildid = "" ; string nodeparentid = "" ; string nodechild = "" ; string nodeparent = "" ; string nodetitle = "" ; /// <summary> /// /// </summary> List<TreeListViewItem> getList = new List<TreeListViewItem>(); /// <summary> /// /// </summary> /// <returns></returns> DataTable setList() { DataTable dt = new DataTable(); dt.Columns.Add( "Id" , typeof ( int )); dt.Columns.Add( "Name" , typeof ( string )); dt.Columns.Add( "ParentId" , typeof ( int )); dt.Columns.Add( "Level" , typeof ( int )); dt.Rows.Add(1, "六福集团" , 0,0); dt.Rows.Add(2, "广州六福" , 1,1); dt.Rows.Add(3, "北京六福" , 1,1); dt.Rows.Add(4, "上海六福" , 1,1); dt.Rows.Add(5, "深圳六福" , 1,1); dt.Rows.Add(6, "人事部" , 5,2); dt.Rows.Add(7, "科技部" , 5,2); dt.Rows.Add(8, "恒大集团" , 0, 0); dt.Rows.Add(9, "恒大广州" , 8, 1); dt.Rows.Add(10, "恒大北京" , 8, 1); dt.Rows.Add(11, "恒大上海" , 8, 1); dt.Rows.Add(12, "恒大深圳" , 8, 1); dt.Rows.Add(13, "人事部" , 11, 2); dt.Rows.Add(14, "科技部" , 11, 2); return dt; } /// <summary> /// /// </summary> /// <param name="tr1"></param> /// <param name="parentId"></param> private void setTreeView(TreeView tr1, int parentId) { DataRow[] ds = setList().Select( "ParentId=" + parentId.ToString()); if (ds.Length > 0) { int pId = -1; foreach (DataRow row in ds) { TreeNode node = new TreeNode(); node.Text = row[ "Name" ].ToString(); node.Tag = ( int )row[ "Id" ]; pId = ( int )row[ "ParentId" ]; if (pId == 0) { //添加根节点 tr1.Nodes.Add(node); } else { //添加根节点之外的其他节点 RefreshChildNode(tr1, node, pId); } //查找以node为父节点的子节点 setTreeView(tr1, ( int )node.Tag); } } } /// <summary> /// /// </summary> public void bind() { ColumnHeader head= new ColumnHeader(); head.Text = "Level" ; treeListView1.Columns.Add(head); DataRow[] ds = setList().Select( "ParentId=0" ); foreach (DataRow row in ds) { //第一层 TreeListViewItem itemA = new TreeListViewItem(row[ "Name" ].ToString(), 0); itemA.Expand(); itemA.SubItems.Add(row[ "Id" ].ToString()); itemA.SubItems.Add(row[ "ParentId" ].ToString()); itemA.SubItems.Add(row[ "Level" ].ToString()); itemA.ToolTipText = row[ "ParentId" ].ToString(); treeListView1.Items.Add(itemA); DataRow[] ds1 = setList().Select( "ParentId=" + row[ "Id" ].ToString()); if (ds1.Length > 0) { //第二层 foreach (DataRow row1 in ds1) { if (row[ "Id" ].ToString() == row1[ "ParentId" ].ToString()) { TreeListViewItem item = new TreeListViewItem(row1[ "Name" ].ToString(), 1); item.SubItems.Add(row1[ "Id" ].ToString()); item.SubItems.Add(row1[ "ParentId" ].ToString()); item.SubItems.Add(row1[ "Level" ].ToString()); item.ToolTipText = row1[ "ParentId" ].ToString(); itemA.Items.Add(item); DataRow[] ds2 = setList().Select( "ParentId=" + row1[ "Id" ].ToString()); if (ds2.Length > 0) { foreach (DataRow row2 in ds2) { //第三层 if (row1[ "Id" ].ToString() == row2[ "ParentId" ].ToString()) { TreeListViewItem item2 = new TreeListViewItem(row2[ "Name" ].ToString(), 2); item2.SubItems.Add(row2[ "Id" ].ToString()); item2.SubItems.Add(row2[ "ParentId" ].ToString()); item2.SubItems.Add(row2[ "Level" ].ToString()); item2.ToolTipText = row2[ "ParentId" ].ToString(); item.Items.Add(item2); } } } } } } } } /// <summary> /// 处理根节点的子节点 /// </summary> /// <param name="tr1"></param> /// <param name="treeNode"></param> /// <param name="parentId"></param> private void RefreshChildNode(TreeView tr1, TreeNode treeNode, int parentId) { foreach (TreeNode node in tr1.Nodes) { if (( int )node.Tag == parentId) { node.Nodes.Add(treeNode); return ; } else if (node.Nodes.Count > 0) { FindChildNode(node, treeNode, parentId); } } } /// <summary> /// 处理根节点的子节点的子节点 /// </summary> /// <param name="tNode"></param> /// <param name="treeNode"></param> /// <param name="parentId"></param> private void FindChildNode(TreeNode tNode, TreeNode treeNode, int parentId) { foreach (TreeNode node in tNode.Nodes) { if (( int )node.Tag == parentId) { node.Nodes.Add(treeNode); return ; } else if (node.Nodes.Count > 0) { FindChildNode(node, treeNode, parentId); } } } /// <summary> /// /// </summary> public TryTreeListView() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TryTreeListView_Load( object sender, EventArgs e) { setTreeView( this .treeView1, 0); this .treeView1.ExpandAll(); bind(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_ItemCheck( object sender, ItemCheckEventArgs e) { TreeListViewItem item = new TreeListViewItem(); //MessageBox.Show(e.Index.ToString()); // TreeListViewItem //MessageBox.Show(treeListView1.SelectedItems.Count.ToString()); // MessageBox.Show(treeListView1.Items[0].SubItems[0].Text); //MessageBox.Show(treeListView1.GetTreeListViewItemFromIndex(e.Index).Text); if (treeListView1.GetTreeListViewItemFromIndex(e.Index).Checked == false ) { //MessageBox.Show(treeListView1.GetTreeListViewItemFromIndex(e.Index).SubItems[0].Text); //从子节点开始 item = treeListView1.GetTreeListViewItemFromIndex(e.Index); getList.Add(item); } //取消 //if (treeListView1.GetTreeListViewItemFromIndex(e.Index).Checked == true) //{ // MessageBox.Show(treeListView1.GetTreeListViewItemFromIndex(e.Index).SubItems[0].Text); //} //MessageBox.Show(e.Index.ToString()); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_ItemActivate( object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, EventArgs e) { this .textBox1.Text=getList.Count.ToString(); string s = getList[0].FullPath; //节点路径 string l=getList[0].Level.ToString(); string ss = getList[0].Parent.Text; string sb = getList[0].Parent.SubItems[0].Text; Form1 frm = new Form1(); frm.fullpath = getList[0].FullPath; //节点路径 frm.editname = getList[0].Text; frm.id = getList[0].SubItems[1].Text; if (frm.ShowDialog() == DialogResult.OK) { } getList = new List<TreeListViewItem>(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_ItemMouseHover( object sender, ListViewItemMouseHoverEventArgs e) { //MessageBox.Show(e.Item.Text); } /// <summary> /// 编辑前的值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_BeforeLabelEdit( object sender, TreeListViewBeforeLabelEditEventArgs e) { MessageBox.Show(e.Item.Text); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_AfterLabelEdit( object sender, TreeListViewLabelEditEventArgs e) { MessageBox.Show(e.Item.Text); } /// <summary> /// 拖放完成发生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_DragDrop( object sender, DragEventArgs e) { try { // Retrieve the client coordinates of the drop location. Point targetPoint = treeListView1.PointToClient( new Point(e.X, e.Y)); //所要放置的位置 // Retrieve the node at the drop location. TreeListViewItem targetNode = treeListView1.GetItemAt(targetPoint); TreeListViewItem fullNode = treeListView1.GetItemAtFullRow(targetPoint); // draggedNode = (TreeListViewItem)e.Data.GetData(typeof(TreeListViewItem)); //string s = e.Data.GetData(typeof(TreeListViewItem)).ToString(); System.Windows.Forms.TreeListView.DragItemData data = (System.Windows.Forms.TreeListView.DragItemData)e.Data.GetData( typeof (System.Windows.Forms.TreeListView.DragItemData)); draggedNode = (TreeListViewItem)data.DragItems[0]; 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(); // int indx=targetNode.Index; // targetNode.Items.Insert(draggedNode.Index,draggedNode); //treeListView1.Items.Insert(indx, targetNode); draggedNode.Remove(); targetNode.Items.Add(draggedNode); // targetNode.Items.Insert(indx, draggedNode); //父节点,添加移动的子节点 nodechildid = draggedNode.SubItems[1].Text; //子节点 nodeparentid = targetNode.SubItems[1].Text; //父节点 nodechild = draggedNode.Text; //子节点 nodeparent = targetNode.Text; //父节点 nodetitle = draggedNode.ToolTipText; //bookKindListInfo.BookKindID = int.Parse(nodechildid); //bookKindListInfo.BookKindParent = int.Parse(nodeparentid); //bookKindListInfo.BookKindName = nodechild; //bookKindListInfo.BookKindCode = nodetitle; //this.textBox1.Text = nodeparent; //this.textBox2.Text = nodechild; //this.textBox3.Text = nodetitle; //formOperatingStringInfo.NodeMoveUpdated = true; //button1.Text = "移動區域節點"; //administrativeAreaListInfo.AreaID = int.Parse(nodechildid); //administrativeAreaListInfo.AreaParentID = int.Parse(nodeparentid); //administrativeAreaListInfo.AreaName = nodechild; //int k = bookKindListBLL.UpdateBookKindList(bookKindListInfo); //if (k >= 1) //{ // MessageBox.Show("移動书目录成功!"); // InitDataTable(); // BindRoot(); // treeView1.ExpandAll(); //全部展开 //} //else //{ // MessageBox.Show("移動书目录不成功!"); //} /// MessageBox.Show("子节点:" + nodechild + ",父节点:" + nodeparent, "拖动节点", MessageBoxButtons.OK, MessageBoxIcon.Information); } // 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.Items.Add((TreeListViewItem)draggedNode.Clone()); } // Expand the node at the location // to show the dropped node. targetNode.Expand(); } } catch (Exception ex) { ex.Message.ToString(); } } /// <summary> ///Determine whether one node is a parent ///or ancestor of a second node. /// </summary> /// <param name="node1"></param> /// <param name="node2"></param> /// <returns></returns> private bool ContainsNode(TreeListViewItem node1, TreeListViewItem 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); } /// <summary> /// 施放离开边界时发生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_DragLeave( object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_DragEnter( object sender, DragEventArgs e) { e.Effect = e.AllowedEffect; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_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); draggedNode = (TreeListViewItem)e.Item; } // Copy the dragged node when the right mouse button is used. else if (e.Button == MouseButtons.Right) { DoDragDrop(e.Item, DragDropEffects.Copy); draggedNode = (TreeListViewItem)e.Item; } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_DragOver( object sender, DragEventArgs e) { //draggedNode = (TreeListViewItem)e.Data.GetData(typeof(TreeListViewItem)); //Point draggedPoint = treeListView1.PointToClient(new Point(e.X, e.Y)); //draggedNode = treeListView1.GetItemAt(draggedPoint); } /// <summary> /// 双控件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_DoubleClick( object sender, EventArgs e) { string selectedname=treeListView1.SelectedItems[0].Text; Form1 frm = new Form1(); frm.fullpath = treeListView1.SelectedItems[0].FullPath; //节点路径 frm.editname = treeListView1.SelectedItems[0].Text; frm.id = treeListView1.SelectedItems[0].SubItems[1].Text; if (frm.ShowDialog() == DialogResult.OK) { } this .textBox1.Text = selectedname; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeListView1_Leave( object sender, EventArgs e) { if (treeListView1.SelectedItems!= null ) { //让选中项背景色呈现红色 treeListView1.SelectedItems[0].BackColor = Color.Red; //前景色为白色 treeListView1.SelectedItems[0].ForeColor = Color.White; } } /// <summary> /// 添加子节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addToolStripMenuItem_Click( object sender, EventArgs e) { } /// <summary> /// 编辑当前节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void editToolStripMenuItem_Click( object sender, EventArgs e) { } /// <summary> /// 删除当前节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void delToolStripMenuItem_Click( object sender, EventArgs e) { } /// <summary> /// 添加同层节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addLevelToolStripMenuItem_Click( object sender, EventArgs e) { } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
tree
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2017-07-05 MySQL and Sql Server:Getting metadata using sql script (SQL-92 standard)
2013-07-05 Javascript: hash tables in javascript
2013-07-05 Csharp:asp.net CheckBoxList databind