c#树控件treeview_添加treenode节点_选中显示_删除

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient; //引入oracle的连接对象命名空间


namespace learncomboxanddatagridview
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
       
        //窗体加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化树控件的节点层次结构 共计添加二层树状节点结构
            TreeNode tn1 = treeView1.Nodes.Add("科蓝公司");
            TreeNode tn2 = treeView1.Nodes.Add("华为");
            TreeNode tn1sub1 = tn1.Nodes.Add("项目管理部");
            TreeNode tn1sub2 = tn1.Nodes.Add("技术管理部");
            TreeNode tn2sub1 = tn2.Nodes.Add("海外部");
            TreeNode tn2sub2 = tn2.Nodes.Add("国内部");
        }

        //选中树控件treeview不同节点后发生,经测试用afterselect可提取选中节点信息
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.textBox1.Text ="当前选中的节点是:"+ e.Node.Text;
        }

        //测试删除树控件节点
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if ((treeView1.SelectedNode.Text == "科蓝公司") || (treeView1.SelectedNode.Text == "华为"))
                {
                    MessageBox.Show("请选择其子节点进行删除");
                }
                //remove为删除树控件节点方法
                treeView1.Nodes.Remove(treeView1.SelectedNode);
                MessageBox.Show("删除节点成功");
            }
            //学习异常处理模块try catch
            catch (Exception x)
            {
                //弹出异常信息,不至于让程序运行中断,可继续运行
                MessageBox.Show(x.Message);
                //调用窗体加载事件重新初始化树控件
                Form1_Load(sender,e);
            }
        }

    }
}
请使用浏览器的分享功能分享到微信等