How to use VScrollBar Class in FlowLayoutPanel instead of his scrollbar

47 Views Asked by At

I'm trying to use VScrollBar class instead of standard scrollbar. I want to do this because I want to change something in the dimension and color of the scrollbar.

I'm trying to set Minimum, Maximum, SmallChange and LargeChange of my scrollbar control but I can't find a way to set correctly these values.

I did in this way:

vScrollBar.Minimum = 0;
            vScrollBar.Maximum = this.pnlContainer.DisplayRectangle.Height;
            vScrollBar.LargeChange = vScrollBar.Maximum /
                         vScrollBar.Height + this.pnlContainer.Height;
            vScrollBar.SmallChange = 15;

But this.pnlContainer.DisplayRectangle.Height is a good value only if I set AutoScroll=true in the panel but in this way it shows his scrollbar.

I'm getting crazy.

Thank you for any help.

UPDATE

I need to use FlowLayoutPanel and not a generic Panel. Furthermore the Parent of my VScrollBar isn't my FlowLayoutPanel (this is something that now I can't change)

1

There are 1 best solutions below

3
GSmyrlis On

You can always create a custom control that inherits from Panel. This custom control will include your custom VScrollBar. In the custom control, override the AutoScroll property to ensure that it toggles the visibility of the standard scrollbar and your custom VScrollBar as needed.

Here's an example of how you can create such a custom panel control:

using System;
using System.Windows.Forms;

public class CustomPanel : Panel
{
    private VScrollBar vScrollBar;

    public CustomPanel()
    {
        // Initialize your custom VScrollBar
        vScrollBar = new VScrollBar();
        vScrollBar.Dock = DockStyle.Right;
        vScrollBar.Visible = false; // Initially, hide your custom scrollbar
        vScrollBar.Scroll += VScrollBar_Scroll;

        // Add the custom VScrollBar to the control collection
        Controls.Add(vScrollBar);
    }

    // Override the AutoScroll property to control scrollbar visibility
    public new bool AutoScroll
    {
        get { return base.AutoScroll; }
        set
        {
            base.AutoScroll = value;
            vScrollBar.Visible = value; // Show your custom scrollbar if AutoScroll is set to true
        }
    }

    private void VScrollBar_Scroll(object sender, ScrollEventArgs e)
    {
        // Handle scrolling logic here if needed
        // You can update the content position in response to scrolling
        // For example: Handle scrolling of child controls within the panel
        // This depends on your specific use case.
    }
}

With this custom panel control, you can use it in your form like a standard Panel, and it will show your custom VScrollBar when AutoScroll is set to true. The standard scrollbar will be hidden. You can then add child controls to this custom panel and customize its appearance and behavior in the way you want.