But I know that by building the corresponding database structure, and then using the recursive method to achieve
Here is the use of the TreeView
The TreeView component is defined by multiple classes, and the TreeView component is named empty
The "TreeView" class in "System.Windows.Forms" is defined, and the
node, which is the name of the namespace "System.Windows.Forms"
"TreeNode" to define. So when you create a TreeView object in your program,
In fact, it just creates a "container" where the nodes can be placed. And in this container, join
A node is actually added to a node created from the "TreeNode" class.
As well as deleting a node, which is to delete 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, while a cumbersome component, boils down to
Bottom, you can sum up to three basic operations: Join the child node, join the sibling node and delete
Node. Master these three common operations, for the flexible use of the TreeView group in programming
Pieces are very necessary. The following are introduced separately.
(1). Join child nodes:
The so-called child node is the next level node in the selected node. Join the child node
The process is to first locate the bit of the child node to join in the TreeView component
, and then create a node object, and then use the join to the node in the Treeveiw class
Method (that is, the Add () method) to join this node object. Below is the
The specific code for adding a child node to the TreeView1 component:
First, determine if the location in the component is selected
if (Treeview1.selectednode = null)
{
MessageBox.Show ("Please select a node", "Prompt Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Else
{
Creates a node object and initializes the
TreeNode tmp;
TMP = new TreeNode ("node name");
To add a child node to the TreeView component
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. Join the Brothers Festival
The method of point is basically consistent with the method of adding a child node, but only in the final implementation method
There is a slight difference. The specific steps to join the sibling node are first and foremost to make sure to add
Into the location of the sibling node, then define a node object, and finally call the
Joins the sibling node in the TreeView class by joining the node object. Join the Brothers
The biggest difference between a node and a joined child node is the final step. I hope the reader can note
Meaning Here is the specific code to join a sibling node in the TreeView component:
First, determine if the location of the nodes in the component is selected
if (Treeview1.selectednode = null)
{
MessageBox.Show ("Please select a node", "Prompt Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Else
{
Creates a node object and initializes the
TreeNode tmp;
TMP = new TreeNode (TextBox1.Text);
Join sibling nodes in the TreeView component
TREEVIEW1.SELECTEDNODE.PARENT.NODES.ADD (TMP);
Treeview1.expandall ();
}
(3). Delete node:
Deleting a node is deleting the selected node in the TreeView component, and deleting the node can
is a child node, or it can be a sibling node, but regardless of the nature of the node, you must ensure that
The node to be deleted does not have a next-level node, or you must first delete all the down in this node
First level node, and then delete this node. Deleting a node is more obvious than the two actions above
Slightly simpler, the specific method is: first of all to determine whether the node to delete the next level
node, if it does not exist, call the Remove () method in the TreeView class to
To delete the node. Here is the specific code to delete the nodes in the TreeView component:
Determines whether the selected node has a next-level node
if (treeView1.SelectedNode.Nodes.Count = 0)
Delete a node
TreeView1.SelectedNode.Remove ();
Else
MessageBox.Show ("Please delete the child nodes in this node first!") "," the Hint letter
", MessageBoxButtons.OK,
MessageBoxIcon.Information);
(4). Some of the other common operations of the TreeView component are:
Other common operations than the above three kinds of operations, in the implementation of the specific
Much simpler. These common operations are simply to expand all nodes and expand the specified section
Point, and collapse all nodes. Here is a concrete introduction:
< I > Expand all nodes:
To expand all nodes in the TreeView assembly, you first need to refer to the selected node
The pins are positioned on the root node of the TreeView component, and then the ExpandAll of the selected component is called
Method is OK, here is the specific code:
Locating the root node
Treeview1.selectednode = treeview1.nodes [0];
Expand all nodes in a component
TreeView1.SelectedNode.ExpandAll ();
< II >. Expand the next level node of the selected node:
Since only the next level of nodes is expanded, there is no need to use the ExpandAll () side
Law. To expand the next level node only need to call the expand () method, the following
is the specific implementation code:
TreeView1.SelectedNode.Expand ();
< III > Collapse all nodes:
Collapse all nodes and expand all nodes is a set of interoperation, the idea of concrete realization
Also roughly the same, folding all nodes is the first to position the selected node pointer in the root
node, and then call the collapse () of the selected component, and the following is the specific
The implementation code:
Locating the root node
Treeview1.selectednode = treeview1.nodes [0];
Collapse all nodes in a component
TreeView1.SelectedNode.Collapse ();
The common methods and general methods for manipulating the TreeView component in C # have thus been basically
All introduced.
Three Example of a fully operational TreeView component written by C #:
The following is an example of a C # written about the TreeView component, in this example
, combined with the common methods and general methods described above, the basic coverage to the TreeView group
Some of the most common operations of a piece. such as the TreeView component in a flexible program
, to add child nodes, sibling nodes, delete nodes, collapse, expand, and other operations.
The first three basic operations are implemented through the function in the pop-up menu in the program, after
The surface operation is accomplished by a button in the program. The following is an abridged code for this program
(TreeView.cs):
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Namespace fully grasp the use of the TreeView component
{
Summary description of the Form1.
public class Form1:form
{
Private TreeView TreeView1;
Private Button button1;
Private Button button2;
Private Button Button3;
Private MenuItem menuItem2;
Private MenuItem menuItem3;
Private MenuItem menuItem4;
Private ContextMenu contextMenu1;
Private TextBox TextBox1;
Private Label Label1;
The required designer variable.
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
Initializing components in a form
InitializeComponent ();
}
Clean up all resources that are in use.
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
private void InitializeComponent ()
{
Initialize Code (abbreviated)
}
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Addchildnode ()
{
First, determine if the location in the component is selected
if (Treeview1.selectednode = null)
{
MessageBox.Show ("Please select a node", "Prompt Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Else
{
if (TextBox1.Text!= "")
{
Creates a node object and initializes the
TreeNode tmp;
TMP = new TreeNode (TextBox1.Text);
To add a child node to the TreeView component
TREEVIEW1.SELECTEDNODE.NODES.ADD (TMP);
Treeview1.selectednode = tmp;
Treeview1.expandall ();
}
Else
{
MessageBox.Show ("TextBox component must fill in node name!") "," the Hint letter
", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
private void Addparent ()
{
First, determine if the location of the nodes in the component is selected
if (Treeview1.selectednode = null)
{
MessageBox.Show ("Please select a node", "Prompt Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Else
{
if (TextBox1.Text!= "")
{
Creates a node object and initializes the
TreeNode tmp;
TMP = new TreeNode (TextBox1.Text);
Join sibling nodes in the TreeView component
TREEVIEW1.SELECTEDNODE.PARENT.NODES.ADD (TMP);
Treeview1.expandall ();
}
Else
{
MessageBox.Show ("TextBox component must fill in node name!") "," the Hint letter
", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
TreeNode tnode = new TreeNode (TextBox1.Text);
}
private void Treeview1_mousedown (object sender,
MouseEventArgs e)
{
if (E.button = = mousebuttons.right)
Contextmenu1.show (This, new Point (e.x, e.y));
}
private void Button1_Click (object sender,
System.EventArgs e)
{
TreeView1.SelectedNode.Expand ();
}
private void Menuitem2_click (object sender,
System.EventArgs e)
{
Addchildnode ();
}
private void Menuitem3_click (object sender,
System.EventArgs e)
{
Addparent ();
}
private void Menuitem4_click (object sender,
System.EventArgs e)
{
Determines whether the selected node has a next-level node
if (treeView1.SelectedNode.Nodes.Count = 0)
Delete a node
TreeView1.SelectedNode.Remove ();
Else
MessageBox.Show ("Please delete the child nodes in this node first!") "," the Hint letter
", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Button2_Click (object sender,
System.EventArgs e)
{
Locating the root node
Treeview1.selectednode = treeview1.nodes [0];
Expand all nodes in a component
TreeView1.SelectedNode.ExpandAll ();
}
private void Button3_Click (object sender,
System.EventArgs e)
{
Locating the root node
Treeview1.selectednode = treeview1.nodes [0];
Collapse all nodes in a component
TreeView1.SelectedNode.Collapse ();
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.