Popular Posts

Wednesday, July 11, 2012

How to make the treeview to scroll to a Position

I had terrible issues with treeview ... eg if you want to select a node in the bottom of the treeview and scrollbars are there then treeview does not scroll to the node ... ie does not ensure the node is visible. This code worked for me:


   Private Const SB_HORZ As Integer = &H0
    Private Const SB_VERT As Integer = &H1
    Private Sub SetTreeViewScrollPos(ByRef treeView As TreeViewByRef scrollPosition As Point)
        Try
            SetScrollPos(DirectCast(treeView.Handle, IntPtr), SB_HORZ, scrollPosition.X, True)
            SetScrollPos(DirectCast(treeView.Handle, IntPtr), SB_VERT, scrollPosition.Y, True)
            If treeView.Nodes.Count > 0 Then
                Dim currentNode As TreeNode = treeView.SelectedNode
                currentNode.EnsureVisible()
            End If
        Catch ex As Exception
 
        End Try
    End Sub
 
    <DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Public Shared Function GetScrollPos(ByVal hWnd As IntegerByVal nBar As IntegerAs Integer
 
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function SetScrollPos(ByVal hWnd As IntPtrByVal nBar As IntegerByVal nPos As IntegerByVal bRedraw As BooleanAs Integer
    End Function


Justin Davis: How to control the scrollbar position in a Tree View Control:

'via Blog this'

No comments:

Post a Comment