Solved

How to remove nodes from a treeview in reverse loop

Posted on 2009-07-07
5
872 Views
Last Modified: 2012-05-07
Hi
Im in the mids of creating some sore of a wizard for my web app. i have a listbox on one side and a treeview on the other. Each time an item is selected from the listbox it is added to the treeview as a node which kinda duplicates the steps you have taken. Anyways what i want to add is the option for the user to click any of the nodes in the treeview so that it can go back to that particular step. Since im adding all the nodes as text and i dont track their location in the treeview its hard to identify which one to remove. So what i basically need is somehow to remove all the nodes up to the one that was selected, going from the last node to the selected one.

This is what i have so far but it deosnt work.
Any ideas?
for (int i = trvSelections.Nodes.Count-1 ; trvSelections.Nodes[i].Value.Equals(trvSelections.SelectedNode.Text); i--)

        {

            trvSelections.Nodes.Remove(trvSelections.Nodes[i]);

        }

Open in new window

0
Comment
Question by:urir10
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 500 total points
ID: 24798228
Compare the following to see what is wrong with your code:
        for (int i = tv.Nodes.Count - 1; !tv.Nodes[i].Value.Equals(tv.SelectedNode.Text); i--)
        {
            tv.Nodes.Remove(tv.Nodes[i]);
        }

Alternately you could do this:
        for (int i = tv.Nodes.Count - 1; i > 0; i--)
        {
            if (tv.Nodes[i].Value.Equals(tv.SelectedNode.Text))
            { break; }
            tv.Nodes.Remove(tv.Nodes[i]);
        }

Alternately you could do lots of things though :)
0
 
LVL 7

Author Comment

by:urir10
ID: 24798294
Thanks it worked great!

One other question, how can i deselect the current selected node?
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24798426
tv.SelectedNode.Selected = false;
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24798465
That's 2 fer 1 :)

Don't leave me hangin on the points :(

I'll do like an infommercial and give you 3...its harder to set the selectedvalue of a particular treeview programmatically, but if you decide you need to see this:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24532209.html
0
 
LVL 7

Author Closing Comment

by:urir10
ID: 31600766
awesome thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now