Search code examples
c#winformslistviewscrolltabpage

How to pass C# ListView scroll to parent container


In my C# Windows Forms application, I have a form which contains a System.Windows.Forms.TabPage that contains multiple System.Windows.Forms.ListViews. The TabPage has a vertical scrollbar.

While scrolling through this TabPage, when the mouse is hovering one of the ListViews, the scrolling stops.

What would be the best way to solve this issue and allow to continue scrolling when the mouse is hovering one of the ListViews?


Solution

  • After searching for a bit, this was my solution:

    I created a new control with the following code:

    using System;
    using System.Windows.Forms;
    
    namespace MyApplication.Controls
    {
        public class ScrollableListView : ListView
        {
            [System.Runtime.InteropServices.DllImport("User32.dll")]
            public static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    
            protected override void WndProc(ref Message m)
            {
                const int WM_MOUSEWHEEL = 0x020A;
                switch (m.Msg)
                {
                    case WM_MOUSEWHEEL:
                        if (m.HWnd == Handle)
                        {
                            if (Parent is TabPage)
                                PostMessage((Parent as TabPage).Handle, m.Msg, m.WParam, m.LParam);
                        }
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }
            }
        }
    }
    

    When the mousewheel is used on this control, the event will be sent to the parent control, if the parent control is a System.Windows.Forms.TabPage.