EnsureVisible in TreeView: how to ensure that horiz. scrollbar is aligned left


I have a tree view with eg. following nodes

# People
+--# Custumers
     +-- # Anne Charles, Hillroad 14, 7766 MyTown, My Country
     +-- # Steve Jones, Forestroad 12, 7766 MyTown, My Country

With the command


I can ensure that "Anne Charles" is visible within the treeview. My problem is: If the treeview is not wide enough to show the complete node text of this node, the horizontal scroll bar is automatically moved to the right so that node structure shown left from the "A" in the text "Anne Charles" is not visible any more. Thus

    Anne Charles, Hillroad 14, 7766 MyTown, My Country
    Steve Jones, Forestroad 12, 7766 MyTown, My Country

is visible and the node structure is hidden.

Question: How can I ensure that the node "Anne Charles" is visible and also the horizontal scrollbar is aligned to the left, which means that the node structure left from the text is still visible?

Best wishes

[Visual Basic .net 2005]
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Use the SetScrollInfo() API like this:
Public Class Form1
    Private Const SB_VERT As Integer = 1
    Private Const SB_HORZ As Integer = 0
    Private Const SIF_PAGE As Integer = &H2
    Private Const SIF_POS As Integer = &H4
    Private Const SIF_RANGE As Integer = &H1
    Private Const SIF_TRACKPOS As Integer = &H10
    Private Const SIF_ALL As Integer = _
    Private Structure SCROLLINFO
        Public cbSize As Integer
        Public fMask As Integer
        Public nMin As Integer
        Public nMax As Integer
        Public nPage As Integer
        Public nPos As Integer
        Public nTrackPos As Integer
    End Structure
    Private Declare Function SetScrollInfo Lib "user32" _
        (ByVal hWnd As IntPtr, ByVal n As Integer, _
        ByRef lpcScrollInfo As SCROLLINFO, ByVal bool As Boolean) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sci As SCROLLINFO
        sci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(sci)
        sci.fMask = SIF_ALL
        sci.nPos = 0 ' <--- this makes it go all the way to the left
        SetScrollInfo(treeViewM.Handle, SB_HORZ, sci, True)
    End Sub
End Class

Open in new window

mp45Author Commented:

Does it mean there does not exist a solution within the .net-framework, e.g. a treeview method I just have not realized yet?

Mike TomlinsonMiddle School Assistant TeacherCommented:
No...I didn't see anyway to do it via built-in methods...
(hopefully I didn't miss it too!)
mp45Author Commented:
thanks a lot.
I've found this solution in addition:

Mike TomlinsonMiddle School Assistant TeacherCommented:
Kewl...so essentially the same approach but with a different API.  =)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.