none
treeview scrollbar event (detecting/controlling)

    Question

  • I have been looking extensively online to find anyway to detect if the scrollbar moves on a treeview but I haven't found anything yet. The overall goal is to sync up a datagridview and a treeview.  I already have it so that when the datagridview scrolls, the treeview scrolls with it by adjusting the topNode property of the tree.  Unfortunately I can't detect if the topNode changes to conversley scroll the datagridview.


    Anyone have an idea on how to do this?

    Thanks much!
    Wednesday, June 20, 2007 8:44 PM

Answers

  • using System;
    using System.Windows.Forms;

    public class MyTreeView : TreeView {
      // Event declaration
      public delegate void ScrollEventHandler(object sender, ScrollEventArgs e);
      public event ScrollEventHandler Scroll;
      private TreeNode mLastTop;
      // WM_VSCROLL message constants
      private const int WM_VSCROLL = 0x0115;
      private const int SB_THUMBTRACK = 5;
      private const int SB_ENDSCROLL = 8;

      protected override void WndProc(ref Message m) {
        // Trap the WM_VSCROLL message to generate the Scroll event
        base.WndProc(ref m);
        if (Scroll != null && m.Msg == WM_VSCROLL && this.TopNode != mLastTop) {
          int nfy = m.WParam.ToInt32() & 0xFFFF;
          if (nfy == SB_THUMBTRACK || nfy == SB_ENDSCROLL) {
            Scroll.Invoke(this, new ScrollEventArgs(this.TopNode, nfy == SB_THUMBTRACK));
            mLastTop = this.TopNode;
          }
        }
      }
      public class ScrollEventArgs {
        // Scroll event argument
        private TreeNode mTop;
        private bool mTracking;
        public ScrollEventArgs(TreeNode top, bool tracking) {
          mTop = top;
          mTracking = tracking;
        }
        public TreeNode Top {
          get { return mTop; }
        }
        public bool Tracking {
          get { return mTracking; }
        }
      }
    }

    Sadly, while testing I found out there is no scroll message when you collapse or expand a node and the tree scrolls because of that.  Bummer.  You can always use a Timer to monitor the TopNode property.
    Monday, June 25, 2007 9:45 AM
    Moderator

All replies

  • Try with Scroll Event. If you can't find it in Form Designer properties window, then type it directly in the form designer source.
    Wednesday, June 20, 2007 9:55 PM
  • I have tried to do that actually. When I go into the designer source manually and attempt to add the scroll Event, I only get compiler errors.  Probably because that particular event isn't exposed in the treeview function/property/event list.  One way I thought about working around this was to create a custom event hander for when the topNode variable of the tree changes (I'm not sure this is possible because I've never attempted to create one).
    Thursday, June 21, 2007 2:13 AM
  • Unfortunately, here is no way to get the event that can detect if the top node changed.

    Monday, June 25, 2007 9:19 AM
  • using System;
    using System.Windows.Forms;

    public class MyTreeView : TreeView {
      // Event declaration
      public delegate void ScrollEventHandler(object sender, ScrollEventArgs e);
      public event ScrollEventHandler Scroll;
      private TreeNode mLastTop;
      // WM_VSCROLL message constants
      private const int WM_VSCROLL = 0x0115;
      private const int SB_THUMBTRACK = 5;
      private const int SB_ENDSCROLL = 8;

      protected override void WndProc(ref Message m) {
        // Trap the WM_VSCROLL message to generate the Scroll event
        base.WndProc(ref m);
        if (Scroll != null && m.Msg == WM_VSCROLL && this.TopNode != mLastTop) {
          int nfy = m.WParam.ToInt32() & 0xFFFF;
          if (nfy == SB_THUMBTRACK || nfy == SB_ENDSCROLL) {
            Scroll.Invoke(this, new ScrollEventArgs(this.TopNode, nfy == SB_THUMBTRACK));
            mLastTop = this.TopNode;
          }
        }
      }
      public class ScrollEventArgs {
        // Scroll event argument
        private TreeNode mTop;
        private bool mTracking;
        public ScrollEventArgs(TreeNode top, bool tracking) {
          mTop = top;
          mTracking = tracking;
        }
        public TreeNode Top {
          get { return mTop; }
        }
        public bool Tracking {
          get { return mTracking; }
        }
      }
    }

    Sadly, while testing I found out there is no scroll message when you collapse or expand a node and the tree scrolls because of that.  Bummer.  You can always use a Timer to monitor the TopNode property.
    Monday, June 25, 2007 9:45 AM
    Moderator