1

I'm working on a FlowLayoutPanel that has the capability of loading hundreds/thousands of custom user controls, each having a PictureBox and many text fields/buttons. I was able to implement virtual paging as seen here which works wonderfully, but I had to make some adjustments. The scrollbar inside the FlowLayoutPanel kept disappearing when the data changed, so I had to create a scrollbar outside the FlowLayoutPanel.

The issue I'm running into is this - the scroll bar won't scroll via mouse wheel when my mouse is anywhere outside of it. I tried making a MouseHover event on the FlowLayoutPanel and setting the focus to my scrollbar, but that didn't work. Any ideas on how I could get the scrollbar to scroll anywhere on the form?

Here's the virtual paging event from the link that I implemented:

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        int pageSize = flowLayoutPanel1.ClientSize.Height / 500;
        int v = Math.Min(userControlList.Count, vScrollBar1.Value);

        flowLayoutPanel1.SuspendLayout();
        flowLayoutPanel1.Controls.Clear();
        flowLayoutPanel1.Controls.AddRange(userControlList.Skip((v - 1) * pageSize)
                                                 .Take(pageSize + 1).ToArray());
        flowLayoutPanel1.ResumeLayout();
    }
  • [ScrollableControl.AutoScrollMinSize](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.scrollablecontrol.autoscrollminsize) (so it won't *disappear*) – Jimi Feb 09 '22 at 01:43
  • Since I'm manually creating the scrollbar, shouldn't I have AutoScroll set to false? And if not, does the AutoScrollMinSize of the FLP need to match the parent panel or form for it to not disappear? – YouShallNotPass Feb 09 '22 at 02:06
  • If you set AutoSscrollMinSize to one or both the dimensions +1, the scrollbars will be visible at any time (e.g., if the size of your FLP is (500, 500), setting (0, 501) will cause the vertical scrollbar to be visible all the time); so you don't need another scrollbar. -- If you want to keep your scrollbar, you need to capture WM_MOUSEWHEEL messages before they're sent to the intended recipient, probably one of the UserControls the FLP is hosting. See: [Hiding the Scrollbar while allowing scrolling with the Mouse Wheel in a FlowLayoutPanel](https://stackoverflow.com/a/67855814/7444103) – Jimi Feb 09 '22 at 02:43
  • And [Set Delta in a WM_MOUSEWHEEL message to send with PostMessage](https://stackoverflow.com/a/60213791/7444103) – Jimi Feb 09 '22 at 02:49
  • Thanks! I'll try these out. – YouShallNotPass Feb 09 '22 at 14:56
  • So if I wanted to keep my scrollbar and still scroll while my mouse was inside the FLP, I would need to override WndProc and see if the message came from WM_MOUSEWHEEL? How would I then point to my vScrollBar1_Scroll function if the MouseWheel message was caught? I'm very new to Winforms, so bear with me. – YouShallNotPass Feb 09 '22 at 20:54
  • If the child Controls inside your FLP completely overlap the FLP's ClientArea, you won't receive MouseWhell messages, since the Mouse is over one of the child Controls. That's why I linked those two Q&As. One is to setup IMessageFilter (see the notes) and the other to post messages to other Windows (to *redirect* the message). -- If you're new to WinForms, you have really picked the right task to start with :) That's why I suggested to use the AutoScrollMinSize. Handling custom Scrollbars is not that easy. – Jimi Feb 09 '22 at 22:10
  • I really have :') The reason I'm looking into virtual paging is because my UserControl is quite complex. Thank you again for linking those, @Jimi! – YouShallNotPass Feb 09 '22 at 22:29

0 Answers0