treenode.tag意义

本文介绍了一段使用 C# 编写的代码,该代码能够遍历指定目录下的所有子目录,并将这些子目录的名称添加到 ListView 控件中进行显示。此外,还会将遍历的路径写入到 TextBox 控件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 foreach (string DirName in Directory.GetDirectories((string)NodeDir.Tag))//编历当前分区或文件夹所有目录
                {
                    textBox1.AppendText((string)NodeDir.Tag + '\n');
                    ListViewItem ItemList = new ListViewItem(DirName);
                    ListViewFile.Items.Add(ItemList);
                }

简化下树形菜单加载的代码【 /// <summary> /// 加载数据初始化 /// </summary> private void LoadShowtab() { //dataGridView绑定 table.TableName = "DocData"; table.Columns.Add("DocID"); table.Columns.Add("DocName"); table.Columns.Add("DocAuto"); table.Rows.Add(Guid.NewGuid().ToString("N"), "激活", "未绑定列"); table.Rows.Add(Guid.NewGuid().ToString("N"), "未激活", "未绑定列"); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = table; //下拉框绑定 comboBox1.DataSource = table; comboBox1.ValueMember = "DocID"; comboBox1.DisplayMember = "DocName"; //listview成组 listView1.View = View.Details; listView1.ShowGroups = true; listView1.FullRowSelect = true; listView1.Groups.Add(new ListViewGroup("技术部", HorizontalAlignment.Left)); listView1.Groups.Add(new ListViewGroup("市场部", HorizontalAlignment.Left)); listView1.Columns.Clear(); listView1.Columns.Add("姓名", 120); listView1.Columns.Add("年龄", 80); listView1.Columns.Add("11", 80); ListViewItem item = new ListViewItem(new[] { "1","2","3"}); var group = listView1.Groups.Cast<ListViewGroup>().FirstOrDefault(g => g.Header == "技术部"); if (group != null) { item.Group = group; } listView1.Items.Add(item); //树形菜单递归 trees = new List<treeViewClass>() { new treeViewClass{父节点="A0",节点="A1",Name="食物" }, new treeViewClass{父节点="A1",节点="A_1",Name="馒头" }, new treeViewClass{父节点="A1",节点="A_2",Name="面" }, new treeViewClass{父节点="B0",节点="B1",Name="水果" }, new treeViewClass{父节点="B1",节点="B1_1",Name="榴莲" }, new treeViewClass{父节点="B1_1",节点="B1_2",Name="榴莲核" }, }; // 构建父节点字典(Key为父节点ID,Value为子节点集合) var parentDict = trees .GroupBy(t => t.父节点) .ToDictionary( g => g.Key, g => g.ToList() ); var allNodeIds = new HashSet<string>(trees.Select(t => t.节点)); var trees_ = trees.Where(t => !allNodeIds.Contains(t.父节点)); // 添加根节点(父节点不在所有节点中的项) foreach (var rootItem in trees_) { TreeNode rootNode = new TreeNode(rootItem.Name) { Tag = rootItem.节点 }; treeView1.Nodes.Add(rootNode); BuildChildNodes(rootNode, parentDict); } } private void BuildChildNodes(TreeNode parentNode, Dictionary<string, List<treeViewClass>> parentDict) { var currentId = parentNode.Tag?.ToString(); if (string.IsNullOrEmpty(currentId)) return; if (parentDict.TryGetValue(currentId, out var children)) { foreach (var child in children) { TreeNode childNode = new TreeNode(child.Name) { Tag = child.节点 }; parentNode.Nodes.Add(childNode); BuildChildNodes(childNode, parentDict); // 递归构建子树 } } }】
最新发布
04-01
对一棵初始为空的高度平衡树(AVL树)进行若干插入或删除操作,请输出旋转信息和最后得到的AVL树。 备注: 当有多种旋转方案时,优先选择旋转次数少的方案。 输入格式: 输出包含多组数据。对于每组数据:输入第一行为一个整数 T,表示操作数目;随后 T 行,每行为Insert K(表示插入关键词为K的结点,若树中已有关键词为K的结点,则不插入)或Remove K(表示删除关键词为K的结点,若树中无关键词为K的结点,则不删除),其中K为整数; T 不超过2×10 5 。 输出格式: 对于每组数据,首先输出一行CaseX:表示第X组数据,X≥1,然后输出2部分。 输出的第1部分为平衡的信息。对于每个Insert/Remove操作: 若未引起结点失衡,则不输出任何信息。 若引起了结点失衡从而导致旋转,则依次输出旋转的信息。具体为:首先输出该Insert或Remove操作,其后加一个冒号和一个空格。若该操作引起结点A失衡,则输出"Rebalance subtree rooted at node A on level L ",即平衡以A为根的子树,其中结点A在树的第L层,A为结点的关键词。若平衡以A为根的子树需要单旋转,则接着输出" with X rotation. " ;若平衡以A为根的子树需要双旋转,则输出" with X rotation and Y rotation. " ;其中X和Y为left或right。注意Remove操作可能引起多个点失衡,应对自底向上的每个失衡点都输出旋转信息,即可能输出多句话,每句话后一个空格。 输出的第2部分为最后得到的高度平衡树的中根序列和先根序列,序列中每个整数后一个空格,两个序列之间用空行间隔。 若存在第1部分,则第1部分和第2部分之间用空行间隔。若没有第1部分(给出的所有Insert/Remove操作都没引起旋转操作),则直接输出第2部分。 每组数据之后都有一个空行。 输入样例: 16 Insert 17 Insert 31 Insert 13 Insert 11 Insert 20 Insert 35 Insert 25 Insert 8 Insert 4 Insert 11 Insert 24 Insert 40 Insert 27 Insert 9 Remove 17 Remove 13 13 Insert 5 Insert 3 Insert 8 Insert 2 Insert 4 Insert 7 Insert 10 Insert 1 Insert 6 Insert 9 Insert 11 Insert 12 Remove 4 3 Insert 6 Insert 5 Insert 8 输出样例: Case 1: Insert 8: Rebalance subtree rooted at node 13 on level 1 with right rotation. Insert 24: Rebalance subtree rooted at node 20 on level 2 with right rotation and left rotation. Remove 17: Rebalance subtree rooted at node 24 on level 2 with left rotation. Remove 13: Rebalance subtree rooted at node 11 on level 1 with right rotation. 4 8 9 11 20 24 25 27 31 35 40 20 8 4 11 9 31 25 24 27 35 40 Case 2: Remove 4: Rebalance subtree rooted at node 3 on level 1 with right rotation. Rebalance subtree rooted at node 5 on level 0 with left rotation. 1 2 3 5 6 7 8 9 10 11 12 8 5 2 1 3 7 6 10 9 11 12 Case 3: 5 6 8 6 5 8 #include<iostream> using namespace std; struct TreeNode { int val; int height; TreeNode* left; TreeNode* right; TreeNode(int x) :val(x), height(0), left(NULL), right(NULL) {} }; bool flag; int tag; int Height(Tre
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值