4dots Software CODE BLOG

How to get the Track position of a Scrollbar while scrolling

The problem

We wanted to get the track position of the horizontal scrollbar of a FlowLayoutPanel while scrolling.
To get the horizontal scroll position we could get the FlowLayoutPanel.HorizontalScroll.Value property.
However, its value does not change while we are actually scrolling the panel but its value changes only after we stopped scrolling and the left mouse button is up.
To get the track position of the scrollbar in the Scroll Event of the FlowLayoutPanel we need little API.

Get Track Position while Scrolling C#


We are going to use the API function GetScrollInfo to get the Track position of the scrollbar.
At first add "using System.Runtime.InteropServices;" to the header of your class.
The required API functions and structures are the following :

        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);

        [Serializable, StructLayout(LayoutKind.Sequential)]
        struct SCROLLINFO
            public uint cbSize;
            public uint fMask;
            public int nMin;
            public int nMax;
            public uint nPage;
            public int nPos;
            public int nTrackPos;

        public enum ScrollInfoMask : uint
            SIF_RANGE = 0x1,
            SIF_PAGE = 0x2,
            SIF_POS = 0x4,
            SIF_DISABLENOSCROLL = 0x8,
            SIF_TRACKPOS = 0x10,

Now to get the track position do something like the following :

        private void ShowTrackPosition()
            SCROLLINFO si = new SCROLLINFO();
            si.cbSize = (uint)Marshal.SizeOf(si);
            si.fMask = (uint)ScrollInfoMask.SIF_ALL;

            GetScrollInfo(flowLayoutPanel1.Handle, (int)System.Windows.Forms.Orientation.Horizontal, ref si);

            lblTrackPos.Text = si.nTrackPos.ToString();
            lblScrollPos.Text = flowLayoutPanel1.HorizontalScroll.Value.ToString();

		private void flowLayoutPanel1_Scroll(object sender, ScrollEventArgs e)

Download Demo Project

How to get the Track position of a Scrollbar while scrolling Demo Project

Please Donate!

Please Share!