After you create a new WinForm project, put a treeview control (TreeView1) in the form . Populate the tree nodes based on database data. The structure of the database is as follows:
ID Title p_id
a root node 0
B Sub-node 1 a
C Sub-node 2 a
default the node for p_id=0 is root node a, with 2 child nodes bandcbelow.
The ID column is non-repeating text, and the Name property of the TreeNode is represented in treeView1 ;
The Title is classified as plain text, and the text property of TreeNode is represented in treeView1 ;
The p_id is listed as the parent ID, and in treeView1 represents the TreeNode Tag property.
First, the recursive method of the calendar to add all nodes to treeView1.
The treeView1 node is loaded and built in the WinForm load event , and several custom methods are used to complete the operation of the Add node:
private void creattree () { Treenode node = new treenode (); //defines the root node model.getbypid ("0"); // Customize the method that gets the root node and move to the attribute node. name = model.id; //assigns each property of the class Model to the root node node. Text = model.title; node. Tag = model.p_id; treeview1.nodes.add (node);//nodes as TreeView1 root node ctree (Node,node. Name); //call another method to add another treeview1.expandall () to the root node; //Expand all nodes}
private void ctree (TreeNode node,string  ID)//Current node, Id{ node of the node. name = id; datatable table = PublicTools.AccessHelper.ExecuteDataTable ("select * from codeeditor where p_id = @P_ID ", new oledbparameter (" @P_ID ", id)) //use a custom method to find all child nodes based on the ID of the current node foreach ( Datarow row in table. Rows)//Calendar All child nodes, assign values and add {Treenode n =new treenode ();n.name = (string) row["ID"];n.text = (String) row["Title"]; n.tag = (String) row["p_id"]; node. Nodes.Add (n); Ctree (n,n.name); //Add the Complete tree node with a recursive method;}}
Ii. handling of general events and settings
1.Click event for tree node:treeview1_afterselect, code:
The node label can be edited treeview1.labeledit = true;
2. After the tree node modification completes the event:treeview1_afterlabeledit, code:
The node label cannot be edited treeview1.labeledit = false;
3.The tree node loses focus still highlighted setting:
(1)
Treeview1.hideselection = false; Typically after a tree node is completed
(2)treeview1_drawnode event:
E.drawdefault = true;//Here is just a default value, you can add specific code to redraw the node
Third, theTreeView control and Common members of TreeNode:
AfterSelect// Click event
Treeview.hideselection = False; Allows the selected node to remain highlighted
Node = Treeview.selectednode; the current tree node that is selected
String I = TreeView1.SelectedNode.Index.ToString ()
the index value of the current node, starting at 0, limited to the half-sibling node collection
TreeView1.SelectedNode.Text = " String " ;// Set the text of the current node
string string = TreeView1.SelectedNode.Text; gets the text of the current node
TreeView1.SelectedNode.FullPath.ToString ();
the full path of the current node, starting from the root node to the current node, with a string of "\" concatenated by the node's Text property, and no "\" after the last node. "
TreeView1.SelectedNode.Tag;
additional information about the current node, which can be of various types or objects
TreeView1.SelectedNode.Name: The name of the tree node, also (treenodes collection)treenodecollection Key in the node (key)
TreeView1.SelectedNode.Parent; The parent tree node of the current tree node.
TreeView1.SelectedNode.Level; the depth of the tree view (zero-based)
Iv. in the treeView Control, the member Nodes is actually a Collection of TreeNode, so you can also use the members of the treenodecollection:
TreeNodeCollection class,treenodes Collection Class
TREEVIEW1.NODES.ADD ( string );
adds a node to the end of the tree collection as a string for display text
TREEVIEW1.NODES.ADD (TreeNode);
adds an existing tree node to the end of the tree node collection.
TREEVIEW1.NODES.ADD (String): Creates a new tree node with the specified key and text and adds it to the collection.
TreeView1.Nodes.Find (String Key,bool searchallchildren): Finds the tree node with the specified key, optionally searching for child nodes, and returning a list[].
Beginner C # Programming, TreeView Control Learning (WinForm)