Http://www.cnblogs.com/wts/archive/2007/03/26/688760.html
Two days ago, I was dizzy by winform's Treeview. Now I have time to write down the knowledge I have used. Let me criticize and correct the comrades who used winform's Treeview.
I. Implementation of Treeview dragging (between tree and tree ):
First of all, you have to wait for two minutes, in the case of treeview1 and treeview2. Set its allowdrop attribute to true.
Add drag Function
Private void treeview_itemdrag (Object sender, system. Windows. Forms. itemdrageventargs E)
{
Dodragdrop (E. Item, dragdropeffects. Move );
}
Private void treeview_dragenter (Object sender, system. Windows. Forms. drageventargs E)
{
E. effect = dragdropeffects. move;
}
Private void treeview_dragdrop (Object sender, system. Windows. Forms. drageventargs E)
{
Treenode newnode;
If (E. Data. getdatapresent ("system. Windows. Forms. treenode", false ))
{
Point Pt = (Treeview) sender). pointtoclient (new point (E. X, E. y ));
Treenode destinationnode = (Treeview) sender). getnodeat (PT );
Newnode = (treenode) E. Data. getdata ("system. Windows. Forms. treenode ");
If (destinationnode. Treeview! = Newnode. Treeview)
{
Destinationnode. nodes. Add (treenode) newnode. Clone ());
Destinationnode. Expand ();
}
}
}
This is the implementation of drag in the mainCodeThe following code constructs several nodes during initialization and adds them to the page_load function of form.
Private void mydomainload (Object sender, system. eventargs E)
{
Treenode parentnode1;
Treenode parentnode2;
Parentnode1 = treeview1.nodes. Add ("WTS ");
Parentnode2.tag = "";
Parentnode1.nodes. Add ("WTS1 ");
Parentnode1.nodes [0]. Tag = "";
Parentnode1.nodes. Add ("wts2 ");
Parentnode1.nodes [1]. Tag = "";
Parentnode1.expand ();
Parentnode2 = treeview2.nodes. Add ("WTS ");
Parentnode2.tag = "";
Parentnode2.nodes. Add ("WTS1 ");
Parentnode2.nodes [0]. Tag = "";
Parentnode2.nodes. Add ("wts2 ");
Parentnode2.nodes [1]. Tag = "";
Parentnode2.expand ();
This. treeview1.itemdrag + = new system. Windows. Forms. itemdrageventhandler (this. treeview_itemdrag );
This. treeview2.itemdrag + = new system. Windows. Forms. itemdrageventhandler (this. treeview_itemdrag );
This. treeview1.dragenter + = new system. Windows. Forms. drageventhandler (this. treeview_dragenter );
This. treeview2.dragenter + = new system. Windows. Forms. drageventhandler (this. treeview_dragenter );
This. treeview1.dragdrop + = new system. Windows. Forms. drageventhandler (this. treeview_dragdrop );
This. treeview2.dragdrop + = new system. Windows. Forms. drageventhandler (this. treeview_dragdrop );
}
You can.
II. Dynamically add nodes and interact with XML.
There are many methods. I will not introduce them here. I will post some code. Please criticize and correct them:
Create root node # region create Root Node
Private treenode createnewnode ()
{
Treenode node = new treenode(this.txt title. Text. Trim ());
Node. Tag = "";
Return node;
}
# Endregion
Get data from XML # region get data from XML
Private void getdatafromxml ()
{
// Read data from XML to Treeview
Try
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("menu. xml ");
Xmlnodelist xmlnodes = xmldoc. documentelement. childnodes;
This. treeview1.beginupdate ();
This. treeview1.nodes. Clear ();
Xmlnodetotreenode (xmlnodes, this. treeview1.nodes );
This. treeview1.endupdate ();
}
Catch
{
}
}
# Endregion
Write Data to XML # region write data to XML
Private void insertdataworkflow XML ()
{
// Save the Treeview to the XML file
Xmldocument Doc = new xmldocument ();
Doc. loadxml ("<menu> </menu> ");
Xmlnode root = Doc. documentelement;
Doc. insertbefore (Doc. createxmldeclaration ("1.0", "UTF-8", "yes"), root );
Treenodetoxml (this. treeview1.nodes, root );
Doc. Save ("menu. xml ");
}
# Endregion
Treenodetoxml # region treenodetoxml
Private void treenodetoxml (treenodecollection treenodes, xmlnode)
{
Xmldocument Doc = xmlnode. ownerdocument;
Foreach (treenode in treenodes)
{
Xmlnode element = Doc. createnode ("element", "item ","");
Xmlattribute ATTR = Doc. createattribute ("WTS ");
Treenode. Tag = "";
ATTR. value = treenode. text;
Element. Attributes. append (ATTR );
Element. appendchild (Doc. createcdatasection (treenode. Tag. tostring ()));
Xmlnode. appendchild (element );
If (treenode. nodes. Count> 0)
{
Treenodetoxml (treenode. nodes, element );
}
}
}
# Endregion
Xmlnodetotreenode # region xmlnodetotreenode
Private void xmlnodetotreenode (xmlnodelist xmlnode, treenodecollection treenode)
{
Foreach (xmlnode VaR in xmlnode)
{
If (var. nodetype! = Xmlnodetype. element)
{
Continue;
}
Treenode newtreenode = new treenode ();
Newtreenode. Text = var. attributes ["WTS"]. value;
If (var. haschildnodes)
{
If (var. childnodes [0]. nodetype = xmlnodetype. CDATA)
{
Newtreenode. Tag = var. childnodes [0]. value;
}
Xmlnodetotreenode (var. childnodes, newtreenode. nodes );
}
Treenode. Add (newtreenode );
}
}
# Endregion xmlnodetotreenode
When these functions are added, the next step is the call problem.
These are some stupid methods, and the exception handling is not fully considered. If you want to see these methods, please kindly advise! Thank you!