【问题标题】:URL to treeview [closed]树视图的 URL [关闭]
【发布时间】:2017-02-08 11:20:56
【问题描述】:

我正在尝试用链接填充树视图。

我有这个数组:

String[] Paths = {
          "Home/Girls/Smoll",
          "Home/Girls/Pige",
          "Home/Girls/Manually",
          "Home/Man/Smoll",
          "Home/Man/Pige",
          "index/domain/index",
          "index/ur"
};

我已经按照我想要的方式进行编码了:

但我的问题是数组不是恒定的,可能会更改为另一个值。

【问题讨论】:

  • 使用String.Split()分割/上的字符串,然后迭代数组并检查de节点是否存在。如果没有,请添加它们。
  • 你需要展示你的所作所为,恐怕 Stack Overflow 不存在为你编写代码。

标签: c# winforms treeview


【解决方案1】:

使用递归算法

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

            string[] Paths = {
                "Home/Girls/Smoll",
                "Home/Girls/Pige",
                "Home/Girls/Manualy",
                "Home/Man/Smoll",
                "Home/Man/Pig",
                "index/domain/index",
                "index/ur"
            };
            List<List<string>> splitPaths = Paths.Select(x => x.Split(new char[] { '/' }).ToList()).ToList();
            TreeNode node = new TreeNode();
            RecursiveAdd(splitPaths, node);
            treeView1.Nodes.Add(node);
            treeView1.ExpandAll();
        }
        public void RecursiveAdd(List<List<string>> splitPaths, TreeNode node)
        {
            var groups = splitPaths.GroupBy(x => x[0]).ToList();
            foreach (var group in groups)
            {
                TreeNode childNode = node.Nodes.Add(group.Key);
                List<List<string>> children = group.Where(x => x.Count() > 1).Select(x => x.Skip(1).ToList()).ToList();
                RecursiveAdd(children, childNode);
            }
        }

    }

}

【讨论】:

  • 谢谢!那是工作!你所有的代码都是清晰的,但是这个 | " var groups = splitPaths.GroupBy(x => x[0]).ToList(); "你能解释一下吗
  • 我按当前根文件夹名称分组。稍后在代码中,我在使用 x.skip(1) 获取孩子时删除了根。所以最初的前两组是 Home 和 index。
猜你喜欢
  • 2010-11-02
  • 2010-09-18
  • 2011-07-14
  • 2019-12-18
  • 2011-05-16
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
相关资源
最近更新 更多