Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain scroll position of treeview

Tags:

How can I maintain the scroll position of a treeview control in .NET application? For example, I have a treeview control and go through a process of adding various nodes to it tacking them on to the bottom. During this process, I can scroll through the treeview and view different nodes. The problem is when the process completes, the treeview scrolls to the very bottom.

It appears that calling treenode.Expand() is what is throwing me off track here. When a parent node is expanded, it gets the focus.

Is there a way around this? If I'm looking at a specific node while the process is running, I don't want it to jump around on me when the process is done.

like image 275
Matt Hanson Avatar asked Dec 02 '08 01:12

Matt Hanson


People also ask

What is TreeView control?

A tree-view control is a window that displays a hierarchical list of items, such as the headings in a document, the entries in an index, or the files and directories on a disk. Each item consists of a label and an optional bitmapped image, and each item can have a list of subitems associated with it.

How do you edit TreeView?

TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView class to true. The default value of the property is false. You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node itself.

What is TreeView control in C#?

The TreeView control contains a hierarchy of TreeViewItem controls. It provides a way to display information in a hierarchical structure by using collapsible nodes . The top level in a tree view are root nodes that can be expanded or collapsed if the nodes have child nodes.


1 Answers

I'm not a VB guy but in C# I do it this way:

Some Win32 native functions:

[DllImport("user32.dll",  CharSet = CharSet.Unicode)] public static extern int GetScrollPos(IntPtr hWnd, int nBar);  [DllImport("user32.dll",  CharSet = CharSet.Unicode)] public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);  private const int SB_HORZ = 0x0; private const int SB_VERT = 0x1; 

A method which returns a point for the current scroll position:

private Point GetTreeViewScrollPos(TreeView treeView) {     return new Point(         GetScrollPos(treeView.Handle, SB_HORZ),          GetScrollPos(treeView.Handle, SB_VERT)); } 

A method to set the scroll position:

private void SetTreeViewScrollPos(TreeView treeView, Point scrollPosition) {     SetScrollPos(treeView.Handle, SB_HORZ, scrollPosition.X, true);     SetScrollPos(treeView.Handle, SB_VERT, scrollPosition.Y, true);  } 

Then when you update your tree, do the following:

BeginUpdate(); Point ScrollPos = GetTreeViewScrollPos(treeMain); // write your update code here SetTreeViewScrollPos(treeMain, ScrollPos); EndUpdate(); 
like image 59
Stefan Koell Avatar answered Sep 19 '22 06:09

Stefan Koell