Solved

Iterating through a treeView

Posted on 2006-06-16
5
500 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:axnst2
  • 4
5 Comments
 

Author Comment

by:axnst2
ID: 16919645
I am working in C#
0
 

Author Comment

by:axnst2
ID: 16919647
.NET 2005
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16919943
0
 

Author Comment

by:axnst2
ID: 16920538
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!
0
 

Author Comment

by:axnst2
ID: 16920562
Thanks for your help though Idle!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question