Link to home
Start Free TrialLog in
Avatar of axnst2
axnst2Flag for United States of America

asked on

Iterating through a treeView

Hi Experts,

      Maybe it's just me, but I think that working with the TreeView Control is 10 times harder than it has to be.  Here's what I need to do:

  I need to dynamically populate a treeView.  I DON'T WANT TO USE ANY LINKED LISTS OR ARRAY LISTS OR ANY OF THAT.  My app is way too complicated to make the use of lists practical.  I basically need to check if a certain grand child nood exists, if it does I need to update it, if not I need to create it.  Please keep in mind that I don't know if there's anything in the TreeView yet at all.  It could be completely empty in which case I need to make the Parent, Child, and, GrandChild nodes for that particular record.  If it does exist, then I need to update the grandchild's name.
  Also, How can I put an icon in front of a certaing grandchild node.  Not all of them, just certain ones.

Thanks,
axnst2
Avatar of axnst2
axnst2
Flag of United States of America image

ASKER

I am working in C#
Avatar of axnst2

ASKER

.NET 2005
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of axnst2

ASKER

This is what I endedd up doing:

void FindInTreeView()
        {
            bool equipAlreadyExists = false;
            int locatedEquipIndex = 0;

            if (treeView1.Nodes[0].Nodes.Count > 0)
            {// If TreeView isn't completely empty

                int equipCounter=0;

                // Check to see if Equipment is already in the list
                foreach (TreeNode tn in treeView1.Nodes[0].Nodes)
                {
                    if (tn.Text == tempEquip.IPAddress)
                    {// It exists
                        equipAlreadyExists = true;
                        locatedEquipIndex = equipCounter;
                    }
                    equipCounter++;
                }

                // If it exists, update it
                if (equipAlreadyExists)
                {
                    treeView1.BeginUpdate();
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[0].Text = "Equipment Type: " + tempEquip.EquipType;
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[1].Text = "Comm. Status: " + tempEquip.CommStatus;
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[2].Text = "Comm. Time: " + tempEquip.LastCommTime + " ms";
                    treeView1.EndUpdate();
                }
                else // If equipment doesn't exist, add it
                {
                    treeView1.BeginUpdate();
                    treeView1.Nodes[0].Nodes.Add(tempEquip.IPAddress);
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Equipment Type: " + tempEquip.EquipType);
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Comm. Status: " + tempEquip.CommStatus);
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Comm. Time: " + tempEquip.LastCommTime + " ms");
                    treeView1.EndUpdate();
                }

            }
            else// If TreeView is empty
            {

                // Add new equipment
                treeView1.BeginUpdate();
                treeView1.Nodes[0].Nodes.Add(tempEquip.IPAddress);
                treeView1.Nodes[0].FirstNode.Nodes.Add("Equipment Type: " + tempEquip.EquipType);
                treeView1.Nodes[0].FirstNode.Nodes.Add("Comm. Status: " + tempEquip.CommStatus);
                treeView1.Nodes[0].FirstNode.Nodes.Add("Comm. Time: " + tempEquip.LastCommTime + " ms");
                treeView1.EndUpdate();
            }
            treeView1.ExpandAll();
        }

This works well for me.  TriewWiev still stinks though!
Avatar of axnst2

ASKER

Thanks for your help though Idle!