Getting Started with Visual Studio C# WinForm Development (6): Use of TreeView Control

The TreeView control displays a hierarchy of nodes as a tree.
For example: the top-level directory is the root (C:), each subdirectory under the C disk is a child node, and each subdirectory has its own child node TreeView
properties and methods:

Attributes illustrate
CheckBoxes Indicates whether a checkbox appears next to the node
ImageList Specifies an ImageList object containing node icons. ImageList object is a collection containing Image objects
Nodes Returns the TreeNode collection in the control as a TreeNodeCollection
SelectedNode selected node

Common events:
AfterSelect: Generated when the selected node changes

TreeNode properties and methods:

TreeNode property illustrate
Checked Indicates whether the TreeNode is selected
FirstNode The first node of the specified Nodes collection
FullPath The path of nodes starting from several roots
ImageIndex When selecting a node, specify the index of the image to be displayed in the ImageList of the TreeView
LastNode Specifies the last node in the Nodes collection
NextNode next sibling node
Nodes A collection of TreeNodes contained in the current node
PrevNode previous sibling node
SelectedImageIndex Specifies the index of the image to display in the TreeView's ImageList when a node is selected
Text Specifies the text of the TreeNode
TreeNode method illustrate
Collapse Collapse nodes on load
Expand expand node
ExpandAll Expand all child nodes of a node
GetNodeCount Return the number of child nodes

Example :
Enter a local file directory, and use TreeView to display all folders in the directory

1. Add a Label control, and change the Text property to: "File Path:".
2. Add a TextBox control, elongate it appropriately, and change the Name property to inputTextBox
3. Add a Button control, change the Text property to: "OK", and change the Name property to enterButton
4. Add a TreeView control, adjust the size appropriately, after completion As shown below:
insert image description here

5. Double-click the Button control and write the Click event. The overall procedure is as follows:

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;

using System.IO;//添加

namespace TreeView 控件
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void enterButton_Click(object sender, EventArgs e)
        {
    
    
            directoryTreeView.Nodes.Clear();// 每次确定时需要刷新内容
            string inputText = inputTextBox.Text; // 获得输入框的内容
            // 文件路径存在
            if (Directory.Exists(inputText))
            {
    
    
                TreeNode rootNode = new TreeNode(inputText); // 创建树节点
                directoryTreeView.Nodes.Add(rootNode); // 加入视图
                FindDirectory(inputText, rootNode);  //通过递归函数进行目录的遍历
            }
            // 文件路径不存在
            else
            {
    
    
                MessageBox.Show("输入目录不存在!!!");
                inputTextBox.Clear(); // 当文件目录不存在时清空控件内容
                directoryTreeView.Nodes.Clear();
            }
        }

        // 递归函数 遍历当前目录
        void FindDirectory(string nowDirectory, TreeNode parentNode)
        {
    
    
            try  // 当文件目录不可访问时,需要捕获异常
            {
    
    
                // 获取当前目录下的所有文件夹数组
                string[] directoryArray = Directory.GetDirectories(nowDirectory);
                if (directoryArray.Length > 0)
                {
    
    
                    foreach (string item in directoryArray)
                    {
    
    
                        // 遍历数组,将节点添加到父亲节点的
                        string str = Path.GetFileNameWithoutExtension(item);
                        TreeNode node = new TreeNode(str);
                        parentNode.Nodes.Add(node);
                        FindDirectory(item, node);
                    }
                }
            }
            catch (Exception)
            {
    
    
                parentNode.Nodes.Add("禁止访问");
            }
        }
    }
}

6. Run the program, enter any existing folder path, and click OK:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44788542/article/details/130402961