WindowForm component TreeView

TreeView control Tree control
Introduction: Hierarchical display of label item collection

1. Attributes

2. Method
Add node data volume assembly flashing
BeginUpdate() Prohibit redrawing
//Add node
EndUpdate() Enable redrawing
CollapseAll() Collapse all nodes, ExpandAll() Expand all nodes
GetNodeAt(node ​​object) Get node GetNodeAt(x,y) ) Node coordinates get node

3. Event
AfterCheck—BeforeCheck Check
AfterSelect—BeforeSelect Check
AfterCollapse—BeforeCollapse Expand
AfterExpand—BeforeExpand Collapse
NodeMouseClick Click the node with the mouse

Manually add nodes
1. Node introduction TreeNode
properties: Name-uniquely identifies the text displayed by the node Text Nodes child node collection

Application: Permission Assignment
Insert picture description here

private void FrmTreeView_Load(object sender, EventArgs e)
{
    
    
    treeView1.Nodes.Clear();		//清除所有节点
    //如果一个节点下面有子节点,我是应该先把这个节点添加到treeview中,还是先应该添加好子节点,再添加到treeview?  都可以
    //添加节点过程:创建一个节点,将这个节点添加到treeview的Nodes集合;创建一个节点,将这个节点添加到当前节点的Nodes集合。。。。。。
    //更多的应用不是这样,而采用递归的方式,加载节点及子节点,数据的来源:制作成张表,ParentId---父节点编号

    //创建根节点 
    TreeNode root = new TreeNode();
    root.Name = "root";  			//节点名称
    root.Text = "学生管理系统";		//节点文本
    treeView1.Nodes.Add(root);		//将一个节点添加 到TreeView上
	//子节点(二级节点)
    TreeNode subNode = new TreeNode();
    subNode.Name = "stuManage";
    subNode.Text = "学生管理";
    root.Nodes.Add(subNode);		//添加一个子节点
	//三级节点
    TreeNode thirdNode = new TreeNode();
    thirdNode.Name = "addStudent";
    thirdNode.Text = "新增学生";
    subNode.Nodes.Add(thirdNode);
    thirdNode = new TreeNode();
    thirdNode.Name = "stuList";
    thirdNode.Text = "学生列表";
    subNode.Nodes.Add(thirdNode);
	//二级节点
    subNode = new TreeNode();
    subNode.Name = "classManage";
    subNode.Text = "班级管理";
    root.Nodes.Add(subNode);	//添加一个子节点
	//二级节点
    subNode = new TreeNode();
    subNode.Name = "gradeManage";
    subNode.Text = "年级管理";
    root.Nodes.Add(subNode);	//添加一个子节点
}

Dynamically add node (recursive)
table data: store the relationship data between nodes and sub-nodes, and the hierarchical relationship cannot be seen
. The process of dynamically loading data to the TreeView control
①The table data is obtained
②Create a method: create a node and add it to the TreeView Principle of recursion :Call yourself
(there can be more than one child node)
③Call method to create node is reflected in TreeView-
dynamic data loading node with hierarchical relationship : the node is created and added to TreeView, and then its child nodes are created, and then added to In its Nodes collection
Insert picture description here
Insert picture description here

private void FrmTreeView_Load(object sender, EventArgs e)
{
    
    
    treeView1.Nodes.Clear();//清除所有节点
    //1. 获取数据
    DataTable dtMenus = DBHelper.GetDataTable("select Id,MName,ParentId from MenuInfos", 1);
    //3.调用方法,添加节点
    CreateNode(dtMenus, null, 0);
}

//2.添加节点(递归)
private void CreateNode(DataTable dt,TreeNode pNode,int parentId)
{
    
    
    //1.获取要创建的节点数据
   DataRow[] rows = dt.Select("ParentId=" + parentId);
    if(rows.Length>0)
    {
    
    
        foreach (DataRow  r in rows)
        {
    
    
            //2.节点信息
            TreeNode node = new TreeNode();
            node.Name = r["Id"].ToString();
            node.Text = r["MName"].ToString();
            //3.直接添加到TreeView Nodes  还是添加指定节点的Nodes里? 
            if (pNode != null)
                pNode.Nodes.Add(node);		//子节点
            else
                treeView1.Nodes.Add(node);	//根节点
            //4.判断当前节点下有没有子节点
            CreateNode(dt, node, int.Parse(node.Name));
        }
    }
}

Tick ​​processing of TreeView nodes
1. Check or cancel the parent node, and all its child nodes are consistent with it.
2. As long as one child node is checked, the parent node is checked; if all child nodes are unchecked, the parent node is not Check
Insert picture description here
Insert picture description here

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    
    
   if( e.Action ==TreeViewAction.ByMouse||e.Action==TreeViewAction.ByKeyboard)
    {
    
    
        SetChildNodesState(e.Node);
        SetParentNodeState(e.Node);
    }
}
//递归处理子节点的勾选
private void SetChildNodesState(TreeNode node)
{
    
    
    if (node.Nodes.Count > 0)
    {
    
    
        foreach (TreeNode n in node.Nodes)
        {
    
    
            n.Checked = node.Checked;
            SetChildNodesState(n);
        }
    }
}
//处理父节点
private void SetParentNodeState(TreeNode node)
{
    
    
    TreeNode pNode = node.Parent;		//获取父节点
    if(pNode!=null)
    {
    
    
        bool bl = false;
        foreach (TreeNode n in pNode.Nodes)
        {
    
    
            if (n.Checked)
            {
    
    
                bl = true;
                break;
            }
        }
        pNode.Checked = bl;
        SetParentNodeState(pNode);
    } 
}

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/114196776