Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#treeview

作者:平凡中的不凡   发布日期:2025-05-19   浏览:97

using System;
using System.Windows.Forms;

namespace TreeViewExample
{
    public class Form1 : Form
    {
        private TreeView treeView1;

        public Form1()
        {
            // 初始化TreeView控件
            treeView1 = new TreeView();
            treeView1.Dock = DockStyle.Fill;

            // 添加根节点
            TreeNode rootNode = new TreeNode("Root Node");
            treeView1.Nodes.Add(rootNode);

            // 为根节点添加子节点
            TreeNode childNode1 = new TreeNode("Child Node 1");
            TreeNode childNode2 = new TreeNode("Child Node 2");
            rootNode.Nodes.Add(childNode1);
            rootNode.Nodes.Add(childNode2);

            // 为子节点添加子节点
            TreeNode grandChildNode1 = new TreeNode("Grand Child Node 1");
            TreeNode grandChildNode2 = new TreeNode("Grand Child Node 2");
            childNode1.Nodes.Add(grandChildNode1);
            childNode2.Nodes.Add(grandChildNode2);

            // 将TreeView控件添加到窗体
            this.Controls.Add(treeView1);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

解释说明:

  • TreeView 控件:用于显示层次结构数据,如文件夹和文件。
  • TreeNode:表示 TreeView 中的单个节点。
  • Nodes.Add() 方法:用于向 TreeViewTreeNode 添加子节点。
  • DockStyle.Fill:使 TreeView 控件填充整个窗体。

此示例代码创建了一个简单的 Windows Forms 应用程序,并在其中添加了一个 TreeView 控件。该控件包含一个根节点和两个子节点,每个子节点又包含两个孙子节点。

上一篇:c# 判断字符串是否为空

下一篇:c# taskcompletionsource

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站