Menu

2 Synchornized TreeView controls

There are business scenarios that need to keep two separate TreeView controls synced, one of them can be content comparison. I was surprised that creating a form with two TreeView controls with synchronised Expand/Collapse and Scroll actions cannot be done without P/Invoke. Here a complete solution. This article does not cover how to fill nodes collection for content comparison, does only cover how to reflect changes on one TreeView scroll and node position onto another.

Synced TreeView expand:

Synced TreeView scroll:

First of all we need to get scroll position info from the TreeView control, to do that we need to override WndProc method:

I did not use TreeView.FullPath because I do not expect for TreeView nodes to be the same. I just expect exact same number of elements on each level for both controls. I think the rest is self explanatory, but feel free to ask questions: