The TreeView component is defined by multiple classes, and the TreeView component is defined by the TreeView class in the Namespace "System.Windows.Forms", where the node (that is, nodes) is created by the namespace "TreeNode" in System.Windows.Forms ". So when you create a TreeView object in your program, you simply create a "container" where you can place the nodes. Adding a node to the container is actually adding a node object created from the "TreeNode" class, and deleting a node, or deleting a "TreeNode" node object.
A The program design and operating environment introduced in this paper
(1). Microsoft Windows 2000 Server Edition
(2). Net Framework SDK Official edition
Two C # operates some of the common methods in the TreeView component, as well as specific implementations:
The TreeView component is a cumbersome component, but in the final analysis, it can be summed up as three basic operations: joining subnodes, joining sibling nodes, and deleting nodes. Mastering these three common operations is essential for flexible application of the TreeView component in programming. The following are introduced separately.
(1). Join child nodes:
The so-called child node is the next level node in the selected node. The process of joining a child node is to first locate the child node you want to join in the TreeView component, then create a node object, and then add the node object by using the Add method (that is, the Add () method) to the node in the Treeveiw class. The following is the specific code that adds a child node to the TreeView1 component:
//首先判断是否选定组件中的位置
if ( treeView1.SelectedNode == null )
{
MessageBox.Show ( "请选择一个节点" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
else
{
//创建一个节点对象,并初始化
TreeNode tmp ;
tmp = new TreeNode ( "节点名称" ) ;
//在TreeView组件中加入子节点
treeView1.SelectedNode.Nodes.Add ( tmp ) ;
treeView1.SelectedNode = tmp ;
treeView1.ExpandAll ( ) ;
}
(2). Join Sibling node:
The so-called sibling node is the node at the peer of the selected node. The method of joining sibling node and the method of adding child nodes are basically consistent, but there is a slight difference in the final method of implementation. The specific steps to join a sibling are first to determine where the sibling nodes are to be joined, then to define a node object, and then to call the method that joins the sibling node in the TreeView class to join the node object. The biggest difference between joining a sibling and joining a child node is the final step. I hope the reader will pay attention. Here is the specific code to join a sibling node in the TreeView component:
//首先判断是否选定组件中节点的位置
if ( treeView1.SelectedNode == null )
{
MessageBox.Show ( "请选择一个节点" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
}
else
{
//创建一个节点对象,并初始化
TreeNode tmp ;
tmp = new TreeNode ( textBox1.Text ) ;
//在TreeView组件中加入兄弟节点
treeView1.SelectedNode.Parent.Nodes.Add ( tmp ) ;
treeView1.ExpandAll ( ) ;
}
(3). Delete node:
Deleting a node is deleting the selected node in the TreeView component. The deletion node can be a child node, or it can be a sibling, but regardless of the nature of the node, you must ensure that the node you want to delete does not have the next level of node, or you must first delete all the next-level nodes in the node, and then delete the node. Deleting a node is a little simpler than the two operations above, by first deciding whether the node to be deleted has the next level of nodes, and if not, call the Remove () method in the TreeView class to delete the node. Here is the specific code to delete the nodes in the TreeView component:
//判断选定的节点是否存在下一级节点
if ( treeView1.SelectedNode.Nodes.Count == 0 )
//删除节点
treeView1.SelectedNode.Remove ( ) ;
else
MessageBox.Show ( "请先删除此节点中的子节点!" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;