开发者

How to select certain child node in TreeView, C#

开发者 https://www.devze.com 2022-12-23 16:57 出处:网络
I am having a problem with selecting a certain child node. What I want to achieve: I you have this treeview for examp开发者_JS百科le (one parent with two child nodes):Parent-Child with a value 5-Chil

I am having a problem with selecting a certain child node.

What I want to achieve: I you have this treeview for examp开发者_JS百科le (one parent with two child nodes):

Parent

-Child with a value 5

-Child with a value 2.

I want to add these two values and assign them to Parent node:

Parent result 7

-Child 5

-Child 2.

Of course, a bigger treeview would have several parents and lots of children and they will all add up to one root node.

How can I do this?? pls help.

thx,

Caslav


You could do something like the following. It assumes the value you want is part of the text (the last value after the last space).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TreeViewRecurse
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         RecurseTreeViewAndSumValues(treeView1.Nodes);
      }

      public void RecurseTreeViewAndSumValues(TreeNodeCollection treeNodeCol)
      {
         int tree_node_sum = 0;
         foreach (TreeNode tree_node in treeNodeCol)
         {
            if (tree_node.Nodes.Count > 0)
            {
               RecurseTreeViewAndSumValues(tree_node.Nodes);
            }
            string[] node_split = tree_node.Text.Split(' ');
            string num = node_split[node_split.Length - 1];
            int parse_res = 0;
            bool able_to_parse = int.TryParse(num, out parse_res);
            if (able_to_parse)
            {
               tree_node_sum += parse_res;
            }
         }
         if (treeNodeCol[0].Parent != null)
         {
            string[] node_split_parent = treeNodeCol[0].Parent.Text.Split(' ');
            node_split_parent[node_split_parent.Length - 1] = tree_node_sum.ToString();
            treeNodeCol[0].Parent.Text = string.Join(" ", node_split_parent);
         }
      }
   }
}


private TreeNode SearchTree(TreeNodeCollection nodes, string searchtext)
        {
            TreeNode n_found_node = null;
            bool b_node_found = false;
            foreach (TreeNode node in nodes)
            {
                if (node.Tag.ToString() as string == searchtext)
                {
                    b_node_found = true;
                    n_found_node = node;
                }
                if (!b_node_found)
                {
                    n_found_node = SearchTree(node.Nodes, searchtext);
                }
            }
            return n_found_node;
        }

Source: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21895513.html


I used a modified version of Redburn's answer to find a treenode by name:

    private TreeNode GetNodeByName(TreeNodeCollection nodes, string searchtext)
    {
        TreeNode n_found_node = null;
        bool b_node_found = false;
        foreach (TreeNode node in nodes)
        {
            //show(node.Name + ":" +searchtext);
            if (node.Name == searchtext)
            {
                //show("score!");
                b_node_found = true;
                n_found_node = node;
                return n_found_node;
            }
            if (!b_node_found)
            {
                //show("here");
                n_found_node = f_get_node_by_name(node.Nodes, searchtext);
                if (n_found_node!=null)
                {
                    return n_found_node;
                }
            }
        }
        return null;
    }


Dunno if this matches your request, but this will add all childs > parent node

    private void button2_Click(object sender, EventArgs e)
    {
        int grandTotal = CalculateNodes(this.treeView1.Nodes);
    }
    private int CalculateNodes(TreeNodeCollection nodes)
    {
        int grandTotal = 0;
        foreach (TreeNode node in nodes)
        {
            if (node.Nodes.Count > 0)
            {
                int childTotal = CalculateNodes(node.Nodes);
                node.Text = childTotal.ToString();
                grandTotal += childTotal;
            }
            else
            {
                grandTotal += Convert.ToInt32(node.Text);
            }
        }
        return grandTotal;
    }

you should do some error checking etc etc to make it solid


You could inherit from TreeNode with something like this:

public class TreeNodeEx : TreeNode {
    // only displayed when having no children
    public int Value { get; set; }

    public bool HasChildren {
        get { return Nodes.Count > 0; }
    }

    public int GetSumOfChildren() {
        if (!HasChildren)
            return Value;

        var children = Nodes.Cast<TreeNode>().OfType<TreeNodeEx>();

        int sum = 0;
        foreach (var child in children)
            sum += child.GetSumOfChildren();

        return sum;
    }
}


Like this:

public class TotalingTreeNode : TreeNode
{
    private int _value = 0;

    public int Value 
    { 
        get
        {
           if (this.Nodes.Count > 1)
              return GetTotaledValue();
           else 
               return _value;
        } 
        set
        {
           if (this.Nodes.Count < 1)
              _value = value;
        }
    }

    private int GetTotaledValue()
    {
        foreach (TotalingTreeNode t in this.Nodes.Cast<TotalingTreeNode>())
        {
            _value += t.Value;
        }
        return _value;
    }
}


In WinForms a childnode of a tree knows its Parent. So you can reach the parent at any time using the TreeNode.Parent property. Vice versa every Node knows it's child nodes. You can reach them using Node.Nodes. This collection has an indexer that allows you to access the child nodes using an int or a string.

To find a TreeNode with a special Key use the following code:

treeView.Nodes.Find("nodeKey", true);

You can finde a description of this method at MSDN

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号