Button visibility when scrolling treeview

Lider

Member
Joined
Mar 12, 2021
Messages
9
Programming Experience
Beginner
The form has a button and a TreeView, which has 1 main node and many child nodes.
Tell me how, when scrolling the TreeView, if the main node is hidden, make the button Enabled = false, if when scrolling the TreeView the main node is shown, then the button make Enbled = true
 
Solution
Handle Scroll event and set Enabled based on Nodes(0).IsVisible.

You should also include WM_MOUSEWHEEL in that class:
VB.NET:
Private Const WM_MOUSEWHEEL As Int32 = &H20A
VB.NET:
ElseIf m.Msg = WM_MOUSEWHEEL AndAlso TopNode IsNot mLastTop Then
    RaiseEvent Scroll(Me, New ScrollEventArgs(TopNode, False))
    mLastTop = TopNode
End If
If you want to perform an action when something happens, that means handling the appropriate event. Have you consulted the documentation for the TreeView class in an effort to determine which event you should handle in this case?
 
As far as I know, the standard treeview does not have a scroll event. But I have my own class code that implements the scroll event:
Tell me how to make a button active or inaccessible when the main node appears / hides in the TreeView.
VB.NET:
Imports System
Imports System.Windows.Forms

Public Class MyTreeView
    Inherits TreeView

  ' Event declaration
  Public Delegate Sub ScrollEventHandler(ByVal sender As Object, ByVal e As ScrollEventArgs)
  Public Event Scroll As ScrollEventHandler
  Private mLastTop As TreeNode
  ' WM_VSCROLL message constants
  Private Const WM_VSCROLL As Integer = &H115
  Private Const SB_THUMBTRACK As Integer = 5
  Private Const SB_ENDSCROLL As Integer = 8

  Protected Overrides Sub WndProc(ByRef m As Message)
    ' Trap the WM_VSCROLL message to generate the Scroll event
    MyBase.WndProc(m)
    If ScrollEvent IsNot Nothing AndAlso m.Msg = WM_VSCROLL AndAlso Me.TopNode IsNot mLastTop Then
      Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
      If nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL Then
        ScrollEvent.Invoke(Me, New ScrollEventArgs(Me.TopNode, nfy = SB_THUMBTRACK))
        mLastTop = Me.TopNode
      End If
    End If
  End Sub
  Public Class ScrollEventArgs
    ' Scroll event argument
    Private mTop As TreeNode
    Private mTracking As Boolean
    Public Sub New(ByVal top As TreeNode, ByVal tracking As Boolean)
      mTop = top
      mTracking = tracking
    End Sub
    Public ReadOnly Property Top() As TreeNode
      Get
          Return mTop
      End Get
    End Property
    Public ReadOnly Property Tracking() As Boolean
      Get
          Return mTracking
      End Get
    End Property
  End Class
End Class
 
Last edited by a moderator:
Handle Scroll event and set Enabled based on Nodes(0).IsVisible.

You should also include WM_MOUSEWHEEL in that class:
VB.NET:
Private Const WM_MOUSEWHEEL As Int32 = &H20A
VB.NET:
ElseIf m.Msg = WM_MOUSEWHEEL AndAlso TopNode IsNot mLastTop Then
    RaiseEvent Scroll(Me, New ScrollEventArgs(TopNode, False))
    mLastTop = TopNode
End If
 
Solution
Here is the complete code, in case someone might need it.

Class:
vb.net:
Public Class MyTreeView
    Inherits TreeView

    ' Event declaration
    Public Delegate Sub ScrollEventHandler(ByVal sender As Object, ByVal e As ScrollEventArgs)
    Public Event Scroll As ScrollEventHandler
    Private mLastTop As TreeNode
    ' WM_VSCROLL message constants
    Private Const WM_VSCROLL As Integer = &H115
    Private Const WM_MOUSEWHEEL As Int32 = &H20A
    Private Const SB_THUMBTRACK As Integer = 5
    Private Const SB_ENDSCROLL As Integer = 8

    Protected Overrides Sub WndProc(ByRef m As Message)
        ' Trap the WM_VSCROLL message to generate the Scroll event
        MyBase.WndProc(m)
        If ScrollEvent IsNot Nothing AndAlso m.Msg = WM_VSCROLL AndAlso Me.TopNode IsNot mLastTop Then
            Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
            If nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL Then
                ScrollEvent.Invoke(Me, New ScrollEventArgs(Me.TopNode, nfy = SB_THUMBTRACK))
                mLastTop = Me.TopNode
            End If
        ElseIf m.Msg = WM_MOUSEWHEEL AndAlso TopNode IsNot mLastTop Then
            RaiseEvent Scroll(Me, New ScrollEventArgs(TopNode, False))
            mLastTop = TopNode
        End If
    End Sub

    Public Class ScrollEventArgs
        ' Scroll event argument
        Private mTop As TreeNode
        Private mTracking As Boolean

        Public Sub New(ByVal top As TreeNode, ByVal tracking As Boolean)
            mTop = top
            mTracking = tracking
        End Sub

        Public ReadOnly Property Top() As TreeNode
            Get
                Return mTop
            End Get
        End Property

        Public ReadOnly Property Tracking() As Boolean
            Get
                Return mTracking
            End Get
        End Property
    End Class
End Class

Form:
vb.net:
Private Sub MyTreeView1_Scroll(sender As Object, e As MyTreeView.ScrollEventArgs) Handles MyTreeView1.Scroll
        If MyTreeView1.Nodes(0).IsVisible Then
            Button1.Enabled = True
        Else
            Button1.Enabled = False
        End If
    End Sub
 
why not Button1.Enabled = MyTreeView1.Nodes(0).IsVisible ?
 
Back
Top