How to remove nodes from a treeview in reverse loop

Posted on 2009-07-07
Last Modified: 2012-05-07
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--)




Open in new window

Question by:urir10
  • 3
  • 2
LVL 20

Accepted Solution

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--)

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; }

Alternately you could do lots of things though :)

Author Comment

ID: 24798294
Thanks it worked great!

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

Expert Comment

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

Expert Comment

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:

Author Closing Comment

ID: 31600766
awesome thanks

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 44
Error in JQuery 5 35
IIS issues - Convert to Application - APS.NET v4.0 3 17
remove background quote mark from widget 6 13
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

929 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

8 Experts available now in Live!

Get 1:1 Help Now