Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

treeView Node in VS2005

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys,
    quick question: I have a TreeNode() array, and there are 3 elements in it, with index 0 , 1, 2. Now i want to reverse the order in the TreeNode(), so that the element in index 0 become with index 2 and so on

    0 1 2
    2 1 0

    like that, how can i do this?

  • User profile image
    geekling

    Before I answer this question: are you serious? That's a pretty basic operation. You might want to invest in a basic programming book if you're having troubles.

    Create a new TreeNode array the same size as your original array, iterate backwards through the original array, adding the elements from the original to the new array.

    Start using the new array.

  • User profile image
    Shark_M

    I know how to do this, but i dont want to do this. I was woundering if there is something in the TreeNode array, like .Reverse, or something that would reverse the order in the array. Instead of making a loop and iterate like you said.

    and secondly i am a biggenner in programming world , i dont claim to be an expert:)

  • User profile image
    JoshB

    Of course he's serious, he wouldn't have posted if he wasn't. And I agree with you Shark there should be an operation a little more efficient then looping through the entire array.

    -- It'd be a shame to miss out on a more elegant solution because you didn't post what you thought might be too basic of a question.

  • User profile image
    footballism

    Shark_M wrote:
    I was woundering if there is something in the TreeNode array, like .Reverse, or something that would reverse the order in the array. Instead of making a loop and iterate like you said.


    TreeNodeCollection myTreeNodeCollection = myTreeView.Nodes;
    TreeNode[] nodes = new TreeNode[myTreeNodeCollection.Count];
    myTreeNodeCollection.CopyTo(nodes, 0);
    myTreeNodeCollection.Clear();
    Array.Reverse(nodes);
    myTreeNodeCollection.AddRange(nodes);

    This solution uses the method Array.Reverse as you request, but It seems not so efficient.

    Sheva


  • User profile image
    geekling

    JoshB wrote:
    Of course he's serious, he wouldn't have posted if he wasn't. And I agree with you Shark there should be an operation a little more efficient then looping through the entire array.

    -- It'd be a shame to miss out on a more elegant solution because you didn't post what you thought might be too basic of a question.


    What makes you think there's a more "elegant" or efficient solution? Have you considered the mechanics behind reversing an array or another linear data structure?

  • User profile image
    geekling

    I'm being serious here, I would actually like to know.

  • User profile image
    androidi

    I haven't thought this through so it might not work...

    You could make your own (inheriting might work) treenode collection or view or whatever.. Now I imagine that at some point the collection will be iterated. So in theory you could add a flag "reverse" which cause the elements be given in reverse order when iterating (by the treeview?).

    I guess it comes down to how the treenodecollection is implemented and used by the treeview. It might also be possible to do similar trick for the treeview but easier said than done.

  • User profile image
    geekling

    Hm, I was thinking in the context of there being a "better" way without having to iterate through the whole array.

    That's a pretty interesting proposition, though. Wink

    Thankfully, .NET languages like Boo come with reverse() builtins, so reversing your enumerable object is as simple as reverse(array) Wink

  • User profile image
    ScanIAm

    Look up ISortable and see if you can simply use the .Sort command.  I found this for arrays recently and it makes things much cleaner. 

    I don't have vs2005 docs installed, and I have to take off, but this might help.

    Edit:  And you might want to check here.  There are some pretty cool tricks to be found:

    http://www.syncfusion.com/faq/windowsforms/Default.aspx

  • User profile image
    androidi

    geekling wrote:
    Hm, I was thinking in the context of there being a "better" way without having to iterate through the whole array.

    That's a pretty interesting proposition, though.

    Thankfully, .NET languages like Boo come with reverse() builtins, so reversing your enumerable object is as simple as reverse(array)


    What I was trying to say was that it might be possible to fool the treeview, or whatever that you want to give reverse results to, in this manner: suppose item 2 out of 100 is requested, for reverse output, give item 100-2 instead of the 2 that was requested.

    The trouble is that I highly doubt this will work so simply or at all in the treenodecollection case..

    But if SharkM simply has an ARRAY of nodes it might ... in theory... Perplexed work.. But can you pass an array of nodes to treeview without a copy operation? Won't there be an extra iteration.. I have no idea.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.