日期:2014-06-10  浏览次数:20610 次

一:实现功能,获得选中节点,在选中节点下添加节点,折叠,展开,删除,得到选中节点下checked项,选中根节点其下节点也选中,图标。上图

二:相关代码

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

namespace TreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 事件:获得选中节点的文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_nodeText_Click(object sender, EventArgs e)
        {
            treeView1.HideSelection = false;//将选中的节点显示为选中的状态,默认是隐藏显示状态
            MessageBox.Show(treeView1.SelectedNode.Text);//nodes是节点的集合,每个分支是一个节点


        }
        /// <summary>
        /// 事件:添加节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_addNode_Click(object sender, EventArgs e)
        {
            //treeView1.Nodes.Add("红马車");//在根节点添加
            //MessageBox.Show(treeView1.Nodes.Count.ToString());//得到根节点的数量
            //treeView1.Nodes[0].Nodes[0].Nodes.Add("红马車");//在根节点的第0根节点下的第0子节点下添加
            //treeView1.Nodes[2].Nodes.Add("红马車");//在第2根节点下添加
            if (treeView1.SelectedNode != null)//是否被选中
            {
                treeView1.SelectedNode.Nodes.Add(tb_nodeText.Text);//在选中的子节点下添加
                treeView1.ExpandAll();//自动展开所有的项
            }

        }
        /// <summary>
        /// 事件:自动载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.HideSelection = false;//显示选中的阴影
            treeView1.ExpandAll();//自动展开所有的项
            treeView1.CheckBoxes = true;//显示选中框
            treeView1.Nodes[0].Nodes[1].SelectedImageIndex = 3;
            treeView1.Nodes[0].Nodes[1].ImageIndex = 2;
        }
        /// <summary>
        /// 事件:展开全部节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ExpandAll_Click(object sender, EventArgs e)
        {
            treeView1.ExpandAll();

        }
        /// <summary>
        /// 事件:折叠全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_CollapseAll_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();

        }
        /// <summary>
        /// 事件:展开当前选中的同级子节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_expandNow_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                treeView1.SelectedNode.Expand();
                //treeView1.SelectedNode.ExpandAll();//展开当前选中的所有的子节点
            }
        }
        /// <summary>
        /// 事件:折叠当前同级节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_CollapseNow_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                treeView1.SelectedNode.Collapse();
            }
        }
        /// <summary>
        /// 事件:删除节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_nodeDelete_Click(object sender, EventArgs e)
        {
            //treeView1.Nodes[0].Remove();//删除根节点的第0节点
            treeView1.Nodes[0].Nodes[0].Remove();//第0个根节点下的第0个节点
        }
        /// <summary>
        /// 事件:删除选中的节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_deleteSelectNode_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                treeView1.SelectedNode.Remove();
            }
        }
        /// <summary>
        /// 事件:查看选中节点选中的同级项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_showChecked_Click(object sender, EventArgs e)
        {
            string str = "选中了:";
            if (treeView1.SelectedNode != null)
            {
                foreach (TreeNode tn in treeView1.SelectedNode.Nodes)
                {
                    if (tn.Checked == true)
                    {
                        str += tn.Text;
                    }
                }
                MessageBox.Show(str);
            }
        }
        /// <summary>
        /// 事件:选中根节点该节点的子节点也选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)//只要有选中就会触发
        {
            foreach (TreeNode tn in e.Node.Nodes)
            {
                tn.Checked = e.Node.Checked;//根节点的选中状态给子节点
            }
        }
    }
}