简体   繁体   English

列出 C# 中 TreeView 中父节点的所有子节点

[英]List all child node from parent node in a TreeView in C#

TreeView: TreeView:

在此处输入图像描述

Expected Result:预期结果:

在此处输入图像描述

Hi, I have a problem regarding to the tree view.嗨,我对树视图有疑问。 I was trying to have a list of output like above picture but Im not really know how to do it.我试图有一个 output 的列表,如上图,但我真的不知道该怎么做。 Im really appreaciate for your help, tq.我真的很感谢你的帮助,tq。

what I can suggest you to do is to iterate the treeview add nodes operation using a for and foreach loops to programmatically add nodes.我可以建议您做的是使用 for 和 foreach 循环以编程方式添加节点来迭代 treeview 添加节点操作。

So, assuming the second image you are showing is the text inserted inside a richtextbox control, you can get the text line by line, and using the number that comes before the string as a reference to insert it into the treeview below the right node as child node or as main node.因此,假设您显示的第二张图像是插入到 Richtextbox 控件中的文本,您可以逐行获取文本,并使用字符串前面的数字作为参考将其插入到右侧节点下方的 treeview 中子节点或作为主节点。

So the code should be (based on the second image):所以代码应该是(基于第二张图片):

private void Add_Button_Click(object sender, EventArgs e)
{
    FillTreeView();
}

private void FillTreeView()
{
    Color previousColor;

    string NumBeforeScore = "";
    string NumAfterScore = "";

    String singleLine = String.Empty; // string to store a single line
    string[] RTBlines = RichTextBox.Lines; // get all lines of the richtextbox
    for (int i = 0; i < RTBlines.Length; i++)
    {
        string tempString = RTBlines[i]; // string to temporarily store our line

        if (char.IsNumber(tempString[0]) == true && char.IsPunctuation(tempString[1]) == true) // check if first character is a number and the second one is a punctuation
        {
            singleLine = tempString; // get the line using the indicated index (here the index is the 'i' integer)

            // Example: 2.3 Test (this is our string), now we are going to split this string in two pieces one with the number and one with the text using the space between the two values
            string stringNumber = singleLine.Split(' ')[0]; // this returns the number '2.3'
            string stringName = singleLine.Split(' ')[1]; // this returns the text 'Test'

            // same as before but we are splitting the number using the score,
            // because we know that before the point there is always a number but not always after the point there can be a number
            if (stringNumber != String.Empty && stringNumber.Contains(".") == true)
            {
                NumBeforeScore = stringNumber.Split('.')[0];
                NumAfterScore = stringNumber.Split('.')[1];
            }
            else
            {
                MessageBox.Show("String do not contains numbers or wrong format, make sure the numbers in the list contains a score (.) separator.", "Error");
            }

            if (NumAfterScore != String.Empty && char.IsNumber(NumAfterScore[0]) == true) // check if the second number after separator contains numbers
            {
                if (TreeView.Nodes.Count > 0) // check if treeview already contains any root node
                {
                    foreach (TreeNode node in TreeView.Nodes) // get the root nodes of the treeview
                    {
                        if (node.Text.Contains(NumBeforeScore + ". ") == true) // check if root node contains the number before the score of the line we are adding
                        {
                            if (node.Nodes.Count > 0)
                            {
                                foreach (TreeNode childnode in node.Nodes)
                                {
                                    if (childnode.Text != singleLine)
                                    {
                                        TreeView.BeginUpdate();
                                        node.Nodes.Add(new TreeNode(singleLine)); //finally adding to the root node our child node from the line of the richtextbox
                                        TreeView.Refresh();
                                        TreeView.EndUpdate();
                                        node.Expand();
                                    }
                                    else
                                    {
                                        TreeView.SelectedNode = childnode;
                                        previousColor = TreeView.SelectedNode.BackColor;
                                        TreeView.SelectedNode.BackColor = Color.Red;
                                        MessageBox.Show("The node you are trying to add already exists.");
                                        TreeView.SelectedNode.BackColor = previousColor;
                                        TreeView.SelectedNode = null;
                                    }
                                }
                            }
                            else
                            {
                                TreeView.BeginUpdate();
                                node.Nodes.Add(new TreeNode(singleLine)); //finally adding to the root node our child node from the line of the richtextbox
                                TreeView.Refresh();
                                TreeView.EndUpdate();
                                node.Expand();
                            }
                        }
                    }
                }
                else // and if not contains root nodes
                {
                    MessageBox.Show("No root nodes found inside the treeview, check the list and add a root node");
                }
            }
            else // and if not containing numbers
            {
                if (TreeView.Nodes.Count > 0)
                {
                    foreach (TreeNode node in TreeView.Nodes) // get the root nodes of the treeview
                    {
                        if (node.Text != singleLine && node.Text.Contains(NumBeforeScore + ". ") == false) // check if root node contains the number before the score of the line we are adding
                        {
                            TreeView.BeginUpdate();
                            TreeView.Nodes.Add(new TreeNode(singleLine)); // adding the text of the line as a new root node
                            TreeView.Refresh();
                            TreeView.EndUpdate();
                        }
                        else
                        {
                            TreeView.SelectedNode = node;
                            previousColor = TreeView.SelectedNode.BackColor;
                            TreeView.SelectedNode.BackColor = Color.Red;
                            MessageBox.Show("The node you are trying to add already exists.");
                            TreeView.SelectedNode.BackColor = previousColor;
                            TreeView.SelectedNode = null;
                        }
                    }
                }
                else
                {
                    TreeView.BeginUpdate();
                    TreeView.Nodes.Add(new TreeNode(singleLine)); // adding the text of the line as a new root node
                    TreeView.Refresh();
                    TreeView.EndUpdate();
                }
            }
        }
    }
}

So if you enter the values in the richtextbox as shown in the example of the second image, you will get the desired arrangement in the first image.因此,如果您在 Richtextbox 中输入值,如第二张图像的示例所示,您将在第一张图像中获得所需的排列。

The only requirement is to indicate the root nodes eg: '1.唯一的要求是指示根节点,例如:'1。 rootnode' and child nodes eg:'1.1 childnode'. rootnode' 和子节点,例如:'1.1 childnode'。 I wrote the example so that any string that comes out of this pattern is not considered, given the text in the image you provided, it also contained some extraneous text 'Activity List:'.我编写了这个示例,因此不考虑来自此模式的任何字符串,鉴于您提供的图像中的文本,它还包含一些无关的文本“活动列表:”。

I leave you a link to the zip file containing the WinForm project with the source code if you want to give it a try before integrating it to yours.如果您想在将其集成到您的项目之前尝试一下,我会给您一个指向 zip 文件的链接,该文件包含带有源代码的 WinForm 项目。

Project link: TreeView_Test项目链接: TreeView_Test

I hope I've been of help, let me know if you need clarification.我希望我对您有所帮助,如果您需要澄清,请告诉我。 :-) :-)

Not sure which direction you're going.不知道你要去哪个方向。 I interpreted this as going from the TreeView to something else (TextBox maybe?).我将此解释为从 TreeView 到其他东西(可能是 TextBox?)。

If so, this could be as simple as:如果是这样,这可能很简单:

private void button1_Click(object sender, EventArgs e)
{
    List<String> output = new List<string>();
    output.Add("Activity List:");
    foreach(TreeNode tn in treeView1.Nodes)
    {
        if (tn.Nodes.Count == 0)
        {
            output.Add(tn.Text);
        }
        else
        {
            foreach(TreeNode child in tn.Nodes)
            {
                output.Add(child.Text);
            }
        }
    }
    textBox1.Lines = output.ToArray();
}

Output: Output:

在此处输入图像描述

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

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