Resolved TreeView - detect node position change

AL|EN

New member
Joined
Aug 7, 2019
Messages
4
Programming Experience
1-3
I have TreeView:

C#:
Group01 
 - PackageP1 
    - ComponentC1 
    - ComponentC2 
Group02 
 - PackageP2 
    - ComponentC3 
    - ComponentC4 
Group03 
 - PackageP3 
    - ComponentC5 
    - ComponentC6

The position of the nodes of this treeview can be changed via Drag&Drop, keyboard hotkey and separate Up/Down buttons. How to react when node will change it's position on the TreeView? I didn't find anything which might be Event OnNodeIndexChange.
 
@Sheepings The only ideas which comes to my mind is:
- execute code directly from the events which can manipulate node index
- some kind of databinding relationship / monitoring node index+node path value
 
Hmm, I don't know if data-binding would be wise with nodes that change position. I'd need to ponder on that and check how that would work. So If you're considering data-binding, is this project in WPF or WF?

When I said : What have you tried, thought of or wrote so far? - I was implying you make an attempt to accomplish this, and if you fail, show us your problematic code. We don't write code for people. We only help you with code you've found or written yourself, and I'm not in a habit of throwing code at people until I can see the scope of your ability to understand the language.

Most of the events for the treeview will only bring you so far. I recall doing a project about 10 years ago where a customer required nodes to be able to move around just as you would see a user move around in a chatroom, similarly to Teamspeak3. I assume this is the functionality you're after?

If I get some free time, I will try to source the code I wrote for you; providing you can translate it yourself from VB.Net to C#? But I'm not making any promises as I'd need to route through many projects to find it.
 
Also this matters (emphasis mine):
How to react when node will change it's position on the TreeView?
Do you need to detect before the node position has changed? Or after the node position has changed?

If you really need to detect before the position changes, then your only recourse will be to intercept all the UI interaction events that might move the node.

Also please answer the question above regarding what framework/platform is this question about: WPF, Xamarin, WinForms, WebForms, MVC?
 
Also please answer the question above regarding what framework/platform is this question about: WPF, Xamarin, WinForms, WebForms, MVC?
Please notice that thread is posted in the Windows Forms forum.
 
C# category doesn't have a forum for some of the platforms on post 5. Would it be better to assume or ask, since sometimes they post in the wrong forums?
 
There are for all except Xamarin, when they happen to post in a specific forum you should assume that is correct, unless something indicates that it can't be.
 
Okay, based on that assumption, then the detection for a future change will have to happen in the various UI interaction event handlers to that will allow manipulation of the nodes.

On the other hand, if detection can be delayed until after the changes happen, it can be detected by hooking into the invalidation or painting events assuming the changes are visible within the visible area of the tree view. The alternative would be a timer that periodically checks the state of tree nodes and compares against it's last known state. It's a hack, but likely less code that writing a tree view from scratch and building in the tree structure change events the right way.
 
I don't use TreeViews much so perhaps I'm wrong but I would think that none of those methods are going to occur automatically, i.e. any movement of nodes is going to be done by your code. If so then you already know when a node is moved because you're doing it, so there's no need to detect it separately. Is that not the case?
 
If you really need to detect before the position changes, then your only recourse will be to intercept all the UI interaction events that might move the node.
I need to execute specific code after any kind of node has changed it's position. Sorry if I wasn't clear before.
I don't use TreeViews much so perhaps I'm wrong but I would think that none of those methods are going to occur automatically, i.e. any movement of nodes is going to be done by your code. If so then you already know when a node is moved because you're doing it, so there's no need to detect it separately. Is that not the case?
That's correct, the movement of the nodes is done by my code.

Hmm, I don't know if data-binding would be wise with nodes that change position. I'd need to ponder on that and check how that would work. So If you're considering data-binding, is this project in WPF or WF?

When I said : What have you tried, thought of or wrote so far? - I was implying you make an attempt to accomplish this, and if you fail, show us your problematic code. We don't write code for people. We only help you with code you've found or written yourself, and I'm not in a habit of throwing code at people until I can see the scope of your ability to understand the language.

Most of the events for the treeview will only bring you so far. I recall doing a project about 10 years ago where a customer required nodes to be able to move around just as you would see a user move around in a chatroom, similarly to Teamspeak3. I assume this is the functionality you're after?

If I get some free time, I will try to source the code I wrote for you; providing you can translate it yourself from VB.Net to C#? But I'm not making any promises as I'd need to route through many projects to find it.
My question was rater about "did i miss something oblivious/is there OnNodeIndexChange event which I missed". Now I'm rather confident that there isn't any such thing and I need to simply execute custom code from the parts of my code which move nodes around. This is only a small piece of the puzzle but it's crucial step.

I can look at the VB code but first I will convert it to the c# via some converter because I'm not familiar with VB at all.
 
I can look at the VB code but first I will convert it to the c# via some converter because I'm not familiar with VB at all.
If you're proficient at C# then reading VB shouldn't be too much of an issue, even if writing it isn't a possibility. If you really do need a converter though, Instant C# from Tangible Software Solutions is vastly superior to any online converter.
 
My question was rater about "did i miss something oblivious/is there OnNodeIndexChange event which I missed".
No you didn't miss anything obvious. The WinForms TreeView control is just a thin wrapper around the Win32 API Tree View Common Control. The Tree View Common Control was conceived and created in the early-mid 90's in the Win32 API style to fulfill a specific need for the Win95 Window Explorer and the Win95 mail client. Back then, if you needed to extend a control, you subclassed it and took over the Windows messages that you needed to intercept to get the new behavior you wanted. Microsoft didn't do anything to make the wrapper more modern (unlike the WinForms DataGridView which written from the ground up as pure WinForms control with a modern approach with an eye for extensibility points).
 
Bad news @AL|EN, I don't have that project any more. It appears I deleted most apps older than 5 years. But If I can, I will try to help you build what you need. But you will need to be specific about what you're trying to do, and show what you've wrote so far.
- execute code directly from the events which can manipulate node index
- some kind of databinding relationship / monitoring node index+node path value
  • Are you only monitoring one node or more?
  • Are you in need of monitoring child nodes?
  • Are your nodes subject to move or change in any way, if so; how?
  • What do you need to databind. Please explain the relationship? (Consider not binding if the third bullet is true as it'll likely be a pain in the ass.)
  • What are you doing with the node path and value?
If you can tell us a bit more, that would be helpful.
 
@Sheepings

  • Are you only monitoring one node or more? - one node at the time
  • Are you in need of monitoring child nodes? - yes, If i move root node, the code is executed for each childnode because the position of those are changed across all treenode
  • Are your nodes subject to move or change in any way, if so; how? - move only by user, only via Drag&Drop, keyboard hotkey and separate Up/Down buttons
  • What do you need to databind. Please explain the relationship? (Consider not binding if the third bullet is true as it'll likely be a pain in the ass.) - right now no databind
  • What are you doing with the node path and value? - at this point, I'm using path value to determine if the node is above/below other node (across whole treeview)
Don't worry about the example code. With previous feedback I was able to figure out it by myself. In order to not make things complicated, I've created method and I execute it at the end of event handler which can move nodes up/down at the treeview. That's one of the piece solved, now it's time for real challenge which I will explain in separate post If I will face difficulties.

Everyone, thanks for help.
 
Last edited:
Back
Top Bottom