views:

1120

answers:

7

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

What I want to achieve: I you have this treeview for example (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

A: 

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;
    }
}
the_ajp
Dude, this will just sum up a bunch of zeroes.
Dan Tao
Hehe very true :P
the_ajp
A: 
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

Redburn
A: 

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

crauscher
Yes, I am aware of that. I am going to use that after I reach the specific child, but how to access it!?
Caslav
I have 10+ children and they are not from the same parent node. and the parent nodes (when values add up) act as children again... and so on...This has to be automated (one click soulution) :)
Caslav
A: 

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;
    }
}
Dan Tao
+2  A: 

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);
         }
      }
   }
}
SwDevMan81
A: 

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

riffnl
A: 

Use Composite Design Pattern to achive your result use this link

http://www.dotnetcube.com/post/Design-Patterns-e28093-Using-the-Composite-Pattern-in-C.aspx

saurabh