c# - Scrollbar resetting to the top of the treeView after sorting? -
i have large tree structure, , whenever user adds or removes child node, tree re-sorted. works well, issue this: after sorting, scrollbar automatically reset top of tree. i'd make scrollbar holds (or returns to) position node added or deleted user doesn't have scroll down , find parent node every single time want add or delete something.
i've been trying find way time now, haven't had luck. have tips?
here's method i'm using removing child node, if helps:
private void removefromcategoryevent(object sender, eventargs e) { suspendlayout(); if (treeviewcategories.selectednode != null) { treenode treenode = treeviewcategories.selectednode; treenode parentnode = treenode.parent; if ((settinggroup != null) && (settinggroup.grouprootcategory != null) && (settinggroup.grouprootcategory.settings != null) && (treenode.tag isetting) && (parentnode.tag idevicesettingcategory)) { isetting currentsetting = treenode.tag isetting; (parentnode.tag idevicesettingcategory).settings.remove(currentsetting); treenode.remove(); settinggroup.grouprootcategory.settings.add(currentsetting); treenode settingnode = rootcategorynode.nodes.add(currentsetting.shortname); settingnode.tag = currentsetting; settingnode.imageindex = utilities.settingcategorychildimage; settingnode.selectedimageindex = utilities.settingcategorychildimage; treeviewcategories.sort(); //scrollbar reset happens here } } resumelayout(); }
you can use p/invoke current scroll position, save it, , restore after sorting.
you need following api calls:
[dllimport("user32.dll", charset = system.runtime.interopservices.charset.auto)] public static extern int getscrollpos(int hwnd, int nbar); [dllimport("user32.dll")] static extern int setscrollpos(intptr hwnd, int nbar, int npos, bool bredraw); private const int sb_horz = 0x0; private const int sb_vert = 0x1;
getting current position:
private point gettreeviewscrollpos(treeview treeview) { return new point( getscrollpos((int)treeview.handle, sb_horz), getscrollpos((int)treeview.handle, sb_vert)); }
setting position:
private void settreeviewscrollpos(treeview treeview, point scrollposition) { setscrollpos((intptr)treeview.handle, sb_horz, scrollposition.x, true); setscrollpos((intptr)treeview.handle, sb_vert, scrollposition.y, true); }
Comments
Post a Comment