TreeView使用方法

1.界面

2.后台代码

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //利用循环生成父节点父子节点
        private void bt_Product_Click(object sender, EventArgs e)
        {
            int ParentNum =Convert.ToInt32(tb_ParentNum.Text);//获取父节点个数
            int ChildNum = Convert.ToInt32(tb_ChildNum.Text);//获取子节点个数
            for (int i = 0; i < ParentNum; i++)
            {
                TreeNode node1 = treeView1.Nodes.Add("父节点" + (i + 1).ToString());
                output("添加父节点:" + (i + 1).ToString());

                for (int j = 0; j < ChildNum; j++)
                {
                    TreeNode node2 = new TreeNode("子节点" + (j + 1).ToString());
                    output("添加子节点:" + (j + 1).ToString());
                    node1.Nodes.Add(node2);
                }
            }
        }


        
        private void output(string log)
        {
            if (textBox2.GetLineFromCharIndex(textBox2.Text.Length) > 100)
                textBox2.Text = "";
            textBox2.AppendText(DateTime.Now.ToString("HH:mm:ss    ") + log + "\r\n");
           Console.Write(log);
        }


        //清空所有节点
        private void bt_Clear_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            output("删除全部节点");
        }



        //删除选中的节点
        private void bt_delete_Click(object sender, EventArgs e)
        {
            output("删除所选节点:" + treeView1.SelectedNode);
            treeView1.Nodes.Remove(treeView1.SelectedNode);
        }


        //选中事件
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            output("所选节点为:" + e.Node.Text);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值