Search code examples
c#winformsscrollable

What's the use of HScroll/VScroll in ScrollableControl?


The ScrollableControl class has 2 protected boolean properties: HScroll and VScroll.

As the document says:

Gets or sets a value indicating whether the horizontal scroll bar is visible.

And

AutoScroll maintains the visibility of the scrollbars automatically. Therefore, setting the HScroll or VScroll properties to true have no effect when AutoScroll is enabled.

So I use them like this, but the scrollbar isn't showed:

class MyScrollableControl : ScrollableControl {
    public MyScrollableControl() {
        this.AutoScroll = false;
        this.HScroll = true;
    }
}

If I use the following code, it works:

this.HorizontalScroll.Visible = true;

When I put them torgether, the scrollbar is still invisible, and the values of HScroll and HorizontalScroll.Visible keep False.

this.AutoScroll = false;
this.HScroll = true;
this.HorizontalScroll.Visible = true;

So what is the real use of HScroll and VScroll ?


Update

My code and test

enter image description here


Solution

  • HScroll property does not affect scroll visibility directly, but it prevent Scroll to be hidden with HorizontalScroll.Visible value

    enter image description here

    In case when HorizontalScroll.Visible is set to true than HScroll will also get a value true (see 2nd line in the table)

    In case when AutoScroll is set to true HorizontalScroll.Visible always stays true and HScroll doesnot have any influense (see last 2 lines)

    Make an app with Control that contains 3 buttons with next handler code, and play with it to see what exactly happening there:

    private void button1_Click(object sender, EventArgs e)
    {
        AutoScroll = !AutoScroll;
        SetValues();
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        HScroll = !HScroll;
        SetValues();
    }
    
    private void SetValues()
    {
        button1.Text = $"Auto: {(AutoScroll ? "On" : "Off")}";
        button3.Text = $"HScroll: {(HScroll ? "On" : "Off")}";
        button2.Text = $"Visible: {(HorizontalScroll.Visible ? "On" : "Off")}";
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        HorizontalScroll.Visible = !HorizontalScroll.Visible;
        SetValues();
    }
    

    Usage (AutoScroll = false)

    To Manually show Scroll set HorizontalScroll.Visible to true

    To Manually hide Scroll set HScroll to false and than HorizontalScroll.Visible to false

    Usage (AutoScroll = true)

    HorizontalScroll.Visible is always true and cannot be changed

    HScroll doesnot affects anything

    enter image description here