简体   繁体   English

C#treeView是一个代码,用于将子节点安排为其父节点的最后一个子节点

[英]C# treeView is there is an code to arrange an child node to be the last child node from its parent

i am using treeView to show sequence directory and its subdirectory and files in the treeView on form and i use the following method to load the tree view 我正在使用treeView在表单上的treeView中显示序列目录及其子目录和文件,我使用以下方法加载树视图

in form load : 在表单加载:

        treeView1.Nodes.Clear();
        toolTip1.ShowAlways = true;
        LoadDirectory("C:\\Windows\\System32\\" + inventedName );  

and the following 3 methods to load directory and subdirectory and files 和以下3种方法来加载目录和子目录和文件

    public void LoadDirectory(string Dir)
    {

        DirectoryInfo di = new DirectoryInfo(Dir);

        TreeNode tds = treeView1.Nodes.Add(di.Name);

        tds.Tag = di.FullName;
        //tds.StateImageIndex = 0;
        tds.ImageIndex = 0;
        tds.StateImageIndex = 0;
        tds.SelectedImageIndex = 0;
        LoadFiles(Dir, tds);
        LoadSubDirectories(Dir, tds);
    }

    private void LoadSubDirectories(string dir, TreeNode td)
    {

        string[] subdirectoryEntries = Directory.GetDirectories(dir);          

        // Loop through them to see if they have any other subdirectories  
        foreach (string subdirectory in subdirectoryEntries)
        {

            DirectoryInfo di = new DirectoryInfo(subdirectory);
            TreeNode tds = td.Nodes.Add(di.Name);
            renameNodes(tds);    
            //tds.StateImageIndex = 0;
            tds.Tag = di.FullName;
            tds.ImageIndex = 0;
            tds.StateImageIndex = 0;
            tds.SelectedImageIndex = 0;
            LoadFiles(subdirectory, tds);
            LoadSubDirectories(subdirectory, tds);

        }
    }

    private void LoadFiles(string dir, TreeNode td)
    {
        string[] Files = Directory.GetFiles(dir, "*.pdf");

        // Loop through them to see files  
        foreach (string file in Files)
        {
            FileInfo fi = new FileInfo(file);
            TreeNode tds = td.Nodes.Add(fi.Name);
            tds.Tag = fi.FullName;
            tds.ImageIndex = 1;
            tds.StateImageIndex = 1;
            tds.SelectedImageIndex = 1;

        }
    }

my problem is the subdirectories (folder) have specific names i can not change it for example : 我的问题是子目录(文件夹)有特定的名称,我不能改变它,例如:

> root 
    > parent 
          > 1.0 xxx
          > 1.10 xxx
          > 1.2 xxx
          > 1.3 xxx 
          > 1.4 xxx
          > 1.5 xxx
          > 1.6 xxx
          > 1.7 xxx
          > 1.8 xxx
          > 1.9 xxx

but i need it to be like that 但我需要它像那样

> root 
    > parent 
         > 1.0 xxx
         > 1.2 xxx
         > 1.3 xxx 
         > 1.4 xxx
         > 1.5 xxx
         > 1.6 xxx
         > 1.7 xxx
         > 1.8 xxx
         > 1.9 xxx
         > 1.10 xxx 

the stupid (1.10 xxx) child must be after (1.9 xxx) child and as i told i can not rename the folder that will be wrong is there is any way to send it to be the last child 愚蠢的(1.10 xxx)孩子必须在(1.9 xxx)孩子之后,并且我告诉我无法重命名将错误的文件夹是否有任何方式将其发送为最后一个孩子

thanks for helping me 谢谢你的帮助

I did a very similar solution a few weeks ago using IEquable. 几周前我使用IEquable做了一个非常类似的解决方案。 I sorted the filenames in code below to get correct solution 我在下面的代码中对文件名进行了排序以获得正确的解决方案

   public class Test
    {
        private void LoadFiles(string dir, TreeNode td)
        {
            string[] Files = Directory.GetFiles(dir, "*.pdf");
            Files = Files.Select(x => new MySort(x)).OrderBy(x => x).Select(x => x.filename).ToArray();

            // Loop through them to see files  
            foreach (string file in Files)
            {
                FileInfo fi = new FileInfo(file);
                TreeNode tds = td.Nodes.Add(fi.Name);
                tds.Tag = fi.FullName;
                tds.ImageIndex = 1;
                tds.StateImageIndex = 1;
                tds.SelectedImageIndex = 1;

            }
        }
    }
    public class MySort : IComparable
    {
        private string[] splitvalues { get; set; }
        public string filename { get; set; }

        public MySort(string filename)
        {
            this.filename = filename;
            splitvalues = filename.Split(new char[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

        }
        public int CompareTo(object other)
        {
            MySort otherMySort = (MySort)other;
            int min = Math.Min(this.splitvalues.Length, otherMySort.splitvalues.Length);

            for (int i = 0; i < min; i++)
            {
                string a = this.splitvalues[i];
                string b = otherMySort.splitvalues[i];

                if (a != b)
                {

                    int numberA = 0;
                    int numberB = 0;

                    if (int.TryParse(a, out numberA))
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            int z = numberA.CompareTo(numberB);
                            //a & b are numbers
                            return numberA.CompareTo(numberB);
                        }
                        else
                        {
                            //a number b string
                            return -1;
                        }

                    }
                    else
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            //a string b number
                            return 1;
                        }
                        else
                        {
                            // a string b string
                            return a.CompareTo(b);
                        }
                    }
                }
            }
            return splitvalues.Length.CompareTo(otherMySort.splitvalues.Length);

        }
    }

The code works properly as you can see from test code below. 从下面的测试代码中可以看出,代码正常工作。 I made one small change to add a Sort() method to make it easier to call the code. 我做了一个小改动,添加了一个Sort()方法,以便更容易调用代码。 Everything else is the same. 其他一切都是一样的。 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = { "1.7.1", "1.7.10", "1.7.2", "1.7.3", "1.7.4", "1.7.5", "1.7.6", "1.7.7", "1.7.8", "1.7.9" };
            string[] output = MySort.Sort(input);
        }
    }
    public class MySort : IComparable
    {
        private string[] splitvalues { get; set; }
        public string filename { get; set; }

        public MySort(string filename)
        {
            this.filename = filename;
            splitvalues = filename.Split(new char[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

        }
        public static string[] Sort(string[] input)
        {
            return input.Select(x => new MySort(x)).OrderBy(x => x).Select(x => x.filename).ToArray();
        }
        public int CompareTo(object other)
        {
            MySort otherMySort = (MySort)other;
            int min = Math.Min(this.splitvalues.Length, otherMySort.splitvalues.Length);

            for (int i = 0; i < min; i++)
            {
                string a = this.splitvalues[i];
                string b = otherMySort.splitvalues[i];

                if (a != b)
                {

                    int numberA = 0;
                    int numberB = 0;

                    if (int.TryParse(a, out numberA))
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            int z = numberA.CompareTo(numberB);
                            //a & b are numbers
                            return numberA.CompareTo(numberB);
                        }
                        else
                        {
                            //a number b string
                            return -1;
                        }

                    }
                    else
                    {
                        if (int.TryParse(b, out numberB))
                        {
                            //a string b number
                            return 1;
                        }
                        else
                        {
                            // a string b string
                            return a.CompareTo(b);
                        }
                    }
                }
            }
            return splitvalues.Length.CompareTo(otherMySort.splitvalues.Length);

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM