treeview中節點拖放實現

2021-04-06 12:45:13 字數 3285 閱讀 8732

//經過修改,可以正常工作

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using system.text;

using system.text.regularexpressions;

using system.io;

namespace a

///

/// 清理所有正在使用的資源。

///

protected override void dispose( bool disposing )

}base.dispose( disposing );

}#region windows 窗體設計器生成的**

///

/// 設計器支援所需的方法 - 不要使用**編輯器修改

/// 此方法的內容。

///

private void initializecomponent()

)}),

new system.windows.forms.treenode("節點4", new system.windows.forms.treenode )}),

new system.windows.forms.treenode("節點5", new system.windows.forms.treenode )})})});

this.treeview1.selectedimageindex = -1;

this.treeview1.size = new system.drawing.size(352, 349);

this.treeview1.tabindex = 0;

this.treeview1.afterexpand += new system.windows.forms.treevieweventhandler(this.treeview1_afterexpand);

this.treeview1.aftercollapse += new system.windows.forms.treevieweventhandler(this.treeview1_aftercollapse);

this.treeview1.dragover += new system.windows.forms.drageventhandler(this.treeview1_dragover);

this.treeview1.dragenter += new system.windows.forms.drageventhandler(this.treeview1_dragenter);

this.treeview1.itemdrag += new system.windows.forms.itemdrageventhandler(this.treeview1_itemdrag);

this.treeview1.dragdrop += new system.windows.forms.drageventhandler(this.treeview1_dragdrop);

// // form1

// this.allowdrop = true;

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(352, 349);

this.controls.add(this.treeview1);

this.name = "form1";

this.text = "form1";

this.resumelayout(false);

}#endregion

///

/// 應用程式的主入口點。

///

//如果允許移動,則可以肯定這是可以接受的資料

if(e.effect == dragdropeffects.move)

else if( !isin( overnode, movingnode ) )

}//把被移動的節點作為這個節點的子節點

movingnode.remove();

overnode.nodes.add(movingnode);

//展開這個節點

overnode.expand();

//更改為展開後的圖示

overnode.imageindex = 1;}}

}protected void treeview1_dragenter (object sender, drageventargs e)

else

}else

}protected void treeview1_dragover (object sender, drageventargs e)

//把這一次的節點設為老節點

oldnode = overnode;

//儲存當前滑鼠位置處節點的顏色

oldnodebackcolor = overnode.backcolor;

oldnodeforecolor = overnode.forecolor;

//獲取當前移動的節點

treenode movingnode = (treenode)(e.data.getdata(typeof(treenode)));

//根據當前滑鼠位置處的節點是否為正被拖動的節點分別設定顏色

if(overnode.equals(movingnode))

else}}

}protected void treeview1_itemdrag (object sender, itemdrageventargs e)

//aftercollapse事件方法

protected void treeview1_aftercollapse (object sender, treevieweventargs e)

//afterexpand事件方法

protected void treeview1_afterexpand (object sender, treevieweventargs e)

private treenode findtreenode (int x, int y)

//獲取下乙個可見的節點

node = node.nextvisiblenode;

}return null;

}private bool isin( treenode nodea,treenode nodeb )}}

TreeView中節點勾選設定

本文 很不錯的文章 treeview樹中節點勾選要求 1 不選中乙個節點,則其所有的子節點都不被選中。2 選中乙個節點,則其所有的子節點都被選中。3 當乙個節點的所有子節點都沒有被選中時,該節點也沒有被選中。4 當乙個節點的所有子節點中有乙個被選中時,則該節點也被選中。中對事件引數e.action的...

C 實現TreeView節點拖拽的方法

public form1 void treeview1 dragdrop object sender,drageventargs e else 更新當前拖動的節點擊擇 treeview1.selectednode newmovenode 展開目標節點,便於顯示拖放效果 targenode.expan...

treeview 父子節點聯動

region treeview聯動方法 private void updatecheckstatus treevieweventargs e updates all parent nodes recursively.private void updateallparentnodes treenode...