C# WinForms: Make panel scrollbar invisible

GugaMelkadze picture GugaMelkadze · May 28, 2014 · Viewed 15.8k times · Source

I have a panel1 with AutoScroll = true.I have to make panel1 scroll with btnUp and btnDown. So far I've made what I was asked for

private void btnUpClicked(Object sender, EventArgs e)
{
    if (panel1.VerticalScroll.Value - 55 > 0)
        panel1.VerticalScroll.Value -= 55;
    else  panel1.VerticalScroll.Value = 0;
}

private void btnDownClicked(Object sender, EventArgs e)
{
    panel1.VerticalScroll.Value += 55;
}

But now I need to hide Scrollbar or make it invisible. I tried

panel1.VerticalScroll.Visible = false;

but it doesn't work. Any ideas guys?

Answer

msmolcic picture msmolcic · May 28, 2014

Ok, I've done the working example of this for you. All you have to do is to change the max value depending on the total size of all the items inside your panel.


Form code:

public partial class Form1 : Form
{
    private int location = 0;

    public Form1()
    {
        InitializeComponent();

        // Set position on top of your panel
        pnlPanel.AutoScrollPosition = new Point(0, 0);

        // Set maximum position of your panel beyond the point your panel items reach.
        // You'll have to change this size depending on the total size of items for your case.
        pnlPanel.VerticalScroll.Maximum = 280;
    }

    private void btnUp_Click(object sender, EventArgs e)
    {
        if (location - 20 > 0)
        {
            location -= 20;
            pnlPanel.VerticalScroll.Value = location;
        }
        else
        {
            // If scroll position is below 0 set the position to 0 (MIN)
            location = 0;
            pnlPanel.AutoScrollPosition = new Point(0, location);
        }
    }

    private void btnDown_Click(object sender, EventArgs e)
    {
        if (location + 20 < pnlPanel.VerticalScroll.Maximum)
        {
            location += 20;
            pnlPanel.VerticalScroll.Value = location;
        }
        else
        {
            // If scroll position is above 280 set the position to 280 (MAX)
            location = pnlPanel.VerticalScroll.Maximum;
            pnlPanel.AutoScrollPosition = new Point(0, location);
        }
    }
}

Picture example:

Pic1 Pic2

You have to set AutoScroll option to False on your panel. I hope you understand what I've done and will get your panel running the way you want. Feel free to ask if you have any questions.