C#控件TreeView的应用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TreeView
{
    public partial class frmTreeView : Form
    {
        public frmTreeView()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();//新建新类
            tn.Text = "显示新节点";//节点显示的文本
            treeView1.Nodes.Add(tn);//添加节点

        }

        private void btnAddb_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();//新建新类
            tn.Text = "显示新的子节点";//子节点显示的文本
            treeView1.Nodes[0].Nodes.Add(tn);//添加子点
        }

        private void btnAddbb_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();//新建新类
            tn.Text = "显示新节点";//节点显示的文本
            treeView1.Nodes[0].Nodes[1].Nodes.Add(tn);//添加节点
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();//新建新类
            tn.Text = "显示新节点";//节点显示的文本
            treeView1.SelectedNode.Nodes.Add(tn);//选择任意节点添加
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();//新建新类
            tn.Text = "显示新节点";//节点显示的文本
            treeView1.SelectedNode.Parent.Nodes.Add(tn);//添加同级节点

        }

        private void frmTreeView_Load(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();
            tn.Text = "主干";
            treeView1.Nodes.Add(tn);
        }
    }

你可能感兴趣的:(treeview)