首页 > 代码库 > C#-树视图的实现以及TreeView属性---ShinePans

C#-树视图的实现以及TreeView属性---ShinePans

树视图TreeView 属性及方法




属性说明
Nodes获取分配给树视图控件的树节点集合
PathSeparator获取或设置树节点路径所使用的分隔符串
SelesctedNode获取或设置当树节点选定时所使用的图像列表索引值
ShowNodeToolTips获取或设置一个值,用以指示树图中的树节点是否经过排序
VisibleCount获取树视图控件黄总完全可见的树节点数目
CollapseAll折叠所有的树节点
ExpandAll 展开所有的树节点
GetNodeAt检索位于指定位置的 树节点
GetNodeCount检索分配给树视图控件的树节点数
..............................................................................................................................................................................................



事件说明
AfterCheck在选中树节点复选框后 发生
AfterCollapse在选中树节点复选框后发生
AfterExpand在展开树节点后发生
AfterSelect在选定树节点后发生
NodeMouseClick当用户使用鼠标单击TreeView 时发生
NodeMouseDoubleClick当用户使用鼠标双击TreeNode 时发生

Form1.cs

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 TreeViewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ContextMenuStrip=contextMenuStrip1; //设置树空间的快捷菜单
            TreeNode TopNode=treeView1.Nodes.Add("博主"); //建立第一个顶级节点
            //建立4个基础节点 ,分别表示 4个大的分支
            TreeNode ParentNode1=new TreeNode("家人");
            TreeNode ParentNode2=new TreeNode("朋友");
            TreeNode ParentNode3=new TreeNode("老师");
            TreeNode ParentNode4=new TreeNode("同学");
            //将四个基础节点添加到顶级节点中
            TopNode.Nodes.Add(ParentNode1);
            TopNode.Nodes.Add(ParentNode2);
            TopNode.Nodes.Add(ParentNode3);
            TopNode.Nodes.Add(ParentNode4);
            //建立6个子节点,分别表示6个小的分支
            TreeNode ChildNode1=new TreeNode("ShinePans");
            TreeNode ChildNode2=new TreeNode("爸爸");
            TreeNode ChildNode3=new TreeNode("妈妈");
            TreeNode ChildNode4=new TreeNode("xuzhengmao");
            TreeNode ChildNode5=new TreeNode("秦明");
            TreeNode ChildNode6=new TreeNode("zhangyihui");
            TreeNode ChildNode7=new TreeNode("xuzhengmao");
            TreeNode ChildNode8=new TreeNode("zhangyihui");
            ParentNode1.Nodes.Add(ChildNode1);
            ParentNode1.Nodes.Add(ChildNode2);
            ParentNode1.Nodes.Add(ChildNode3);
            ParentNode4.Nodes.Add(ChildNode4);
            ParentNode3.Nodes.Add(ChildNode5);
            ParentNode4.Nodes.Add(ChildNode6);
            ParentNode2.Nodes.Add(ChildNode7);
            ParentNode2.Nodes.Add(ChildNode8);
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            label1.Text="选择的部门:"+e.Node.Text;
        }

        private void 全部展开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
        }

        private void 全部折叠ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();
        }


        }
    
}


program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TreeViewTest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //树视图
            
        }
    }
}


Form1设计


namespace TreeViewTest
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.全部展开ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.全部折叠ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Location = new System.Drawing.Point(5, 7);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(247, 294);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.全部展开ToolStripMenuItem,
            this.全部折叠ToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(11, 320);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 12);
            this.label1.TabIndex = 2;
            // 
            // 全部展开ToolStripMenuItem
            // 
            this.全部展开ToolStripMenuItem.Name = "全部展开ToolStripMenuItem";
            this.全部展开ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.全部展开ToolStripMenuItem.Text = "全部展开";
            this.全部展开ToolStripMenuItem.Click += new System.EventHandler(this.全部展开ToolStripMenuItem_Click);
            // 
            // 全部折叠ToolStripMenuItem
            // 
            this.全部折叠ToolStripMenuItem.Name = "全部折叠ToolStripMenuItem";
            this.全部折叠ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.全部折叠ToolStripMenuItem.Text = "全部折叠";
            this.全部折叠ToolStripMenuItem.Click += new System.EventHandler(this.全部折叠ToolStripMenuItem_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(260, 361);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.treeView1);
            this.Name = "Form1";
            this.Text = "树视图";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ToolStripMenuItem 全部展开ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 全部折叠ToolStripMenuItem;


    }
}