2-10-3  TreeView控件实践操作

1. 案例学习:TreeView 控件的设计开发

1)从工具箱之中拖放一个textbox控件,一个TreeView控件和三个button控件,如图2-35所示进行排列:
2-35  TreeView 控件设计与开发窗体布局运行效果图
2)再从工具箱的菜单和工具栏之中拖放一个ContentMenuStrip控件,并设置该菜单的三个子菜单名称。该控件主要用于当鼠标右键单击TreeView控件的时候,弹出用户自定义快捷菜单。如图2-36所示。
2-36  配置ContentMenuStrip菜单控件
3)具体功能的实现见下面的编码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication2
{
    public partial class Form012 : Form
    {
        public Form012()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化表单对象时候,设置treeView对象的根节点。
        /// </summary>
        private void Form012_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            TreeNode tem = new TreeNode("根节点");
            treeView1.Nodes.Add(tem);
        }
        /// <summary>
        /// 添加子节点方法代码
        /// </summary>
        private void AddChildNode()
        {
            //首先判断是否选定组件中的位置
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (textBox1.Text != "")
                {
                    //创建一个节点对象,并初始化
                    TreeNode tmp;
                    tmp = new TreeNode(textBox1.Text);
                    //在TreeView组件中加入子节点
                    treeView1.SelectedNode.Nodes.Add(tmp);
                    treeView1.SelectedNode = tmp;
                    treeView1.ExpandAll();
                }
                else
                {
                    MessageBox.Show("TextBox组件必须填入节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }
        /// <summary>
        /// 添加兄弟节点方法代码
        /// </summary>
        private void AddParent()
        {
            try
            {
                //首先判断是否选定组件中节点的位置
                if (treeView1.SelectedNode == null)
                {
                    MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (textBox1.Text != "")
                    {
                        //创建一个节点对象,并初始化
                        TreeNode tmp;
                        tmp = new TreeNode(textBox1.Text);
                        //在TreeView组件中加入兄弟节点
                        treeView1.SelectedNode.Parent.Nodes.Add(tmp);
                        treeView1.ExpandAll();
                    }
                    else
                    {
                        MessageBox.Show("TextBox组件必须填入节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            catch
            {
                TreeNode tem = new TreeNode("根节点");
                treeView1.Nodes.Add(tem);
            }
        }
        /// <summary>
        /// treeView1控件鼠标点击事件:MouseDown,判别如果点击鼠标右键,则弹出快捷菜单
        /// </summary>
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                contextMenuStrip1.Show(this, new Point(e.X, e.Y));
            //获取当前鼠标像素位置为快捷菜单弹出的位置
        }
        /// <summary>
        /// 展开下一节点
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Expand();
        }
        /// <summary>
        /// 展开全部节点
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            //定位根节点
            treeView1.SelectedNode = treeView1.Nodes[0];
            //展开组件中的所有节点
            treeView1.SelectedNode.ExpandAll();
        }
        /// <summary>
        /// 折叠全部节点
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            //定位根节点
            treeView1.SelectedNode = treeView1.Nodes[0];
            //折叠组件中所有节点
            treeView1.SelectedNode.Collapse();
        }
        /// <summary>
        /// 点击快捷菜单中“加入子节点”项,则调用用户自定义的AddChildNode()方法。
        /// </summary>
        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddChildNode();
        }
        /// <summary>
        /// 点击快捷菜单中“加入兄弟节点”项,则调用用户自定义的AddParent()方法。
        /// </summary>
        private void ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            AddParent();
        }
        /// <summary>
        /// 点击快捷菜单中“删除节点”项代码。
        /// </summary>
        private void ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            //判断选定的节点是否存在下一级节点
            if (treeView1.SelectedNode.Nodes.Count == 0)
                //删除节点
                treeView1.SelectedNode.Remove();
            else
                MessageBox.Show("请先删除此节点中的子节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}