This is the 23rd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Preface:

TreeView controls are also used frequently in forms applications. We use TreeView to display resources in a hierarchical manner, similar to the indented representation of trees in data structures. Many companies use the TreeView control as a hierarchical view of the file display information, similar to the directory in Explorer on Windows. Each piece of information in the TreeView control has a Node object associated with it. A TreeView displays a hierarchical directory structure of Node objects, each consisting of a Label object and its associated bitmap. After you create a TreeView control, you can expand and collapse, show, or hide the nodes in it. The TreeView control is typically used to display file and directory structures, class hierarchies in documents, hierarchies in indexes, and other information with a hierarchical directory structure.

Once a day to prevent puppy love

1. Use TreeView control

1.1 Common Properties of the TreeView control

Index Gets the location of the tree node in the tree Nodes Gets the tree node set assigned to the tree view control Parent Gets or sets the Parent container of the control SelectedNode Gets or sets the currently selected tree node ExpandAll in the tree view control expands all tree Nodes Text Gets or sets the Text displayed in the tree node label Expand the tree node Clear Clear the tree Remove Removes the current tree node from the tree view control.Copy the code

1.2 Create a form file and change the name

2. Design the interface

We use simple cases to achieve this effect

3. Add and delete data

3.1 Click the treeView to edit the node

3.2 Adding data in the TreeView editor

Note: we are adding data statically here, the subsequent deletion or addition is also static, the subsequent deletion is not effective

3.3 TreeView control adds the root operation

Note: the addition is not added to the database, it is a static display and is not statically written to the treeView

3.4 TreeView control adds child node operation

We need to get the node that we’ve selected and see if it’s empty, if it’s not empty, we have a root to add, if it’s not, we need to add children.

3.5 Deleting the TreeView control

Here we need to determine whether the parent of the node that we chose is null, null means that the node is the root node and we need to delete the whole node, not null means that the node is the child node, and we need to delete the child node from the parent node.

3.6 TreeView Control Clears the tree

In this case, we can directly use clear to delete. No other operations

4. Overall effect display

4.1 Code Demonstration

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TreeTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { TreeNode treeNode = new TreeNode(textBox1.Text, 2, 2); treeView1.Nodes.Add(treeNode); treeView1.Select(); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { } private void button2_Click(object sender, EventArgs e) { TreeNode selectdnode = treeView1.SelectedNode; if(selectdnode! =null) { TreeNode chnode = new TreeNode(textBox2.Text, 2, 2); selectdnode.Nodes.Add(chnode); selectdnode.Expand(); treeView1.Select(); } else {messagebox.show (" Please select to add child nodes "); } } private void button3_Click(object sender, EventArgs e) { TreeNode selectnode = treeView1.SelectedNode; TreeNode parentnode = selectnode.Parent; if(parentnode == null) { treeView1.Nodes.Remove(selectnode); } else { parentnode.Nodes.Remove(selectnode); } treeView1.Select(); } private void button4_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); } private void button5_Click(object sender, EventArgs e) { this.textBox1.Clear(); this.textBox2.Clear(); }}}Copy the code

Conclusion:

TreeView control explains that bloggers do not use the database for operation, but simply use variables to operate, we use the database is the need to use the depth of the node to establish, each layer may have a table, finally to the bottom is our data, we obtain the Name of each layer spanning tree, to achieve the concave representation of resources, The blogger is just a simple introduction to use, more suitable for beginners, is to write about static data do not connect to the database for operation, if you need to connect to the database to pay attention to the blogger next article may write, well, creation is not easy to praise attention to comment collection, thank you big guy !!!!