简体   繁体   中英

Invert windows forms vertical scrollbar

I'm making a winforms app c#. The vertical scroll bar min value is at the top and max at the bottom, and scrolling down increases the value and vice versa. Is there a way to invert it, so that up is higher and down is lower.

You cannot actually "see" the value of the scroll bar just by looking at it, so, in other words, there is no actual difference between having min at the top, max at the bottom, and then just inverting the value when you access it:

private void ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    // get the value (0 -> 100)
    int value = scrollBar.Value;

    // invert it (100 -> 0)
    value = 100 - value;

    // display it
    someLabel.Text = value.ToString();
}

Of course, you can also override the VScrollBar class and add your own "inverted value" property:

public class InvertedScrollBar : VScrollBar
{
    /// <summary>
    /// Gets or sets the "inverted" scrollbar value.
    /// </summary>
    /// <value>The inverted value.</value>
    public int InvertedValue
    {
        get
        {
            int offset = this.Value - this.Minimum;
            return this.Maximum - offset;
        }
        set
        {
            int offset = this.Maximum - value;
            this.Value = this.Minimum + offset;
        }
    }
}

Note that Maximum still has to be larger than Minimum when configuring it.

The values returned by the Value property of a ScrollBar go from scrollBar.Minimum to scrollBar.Maximum - scrollBar.LargeChange .

Thus if a scroll bar has Minimum of 5, Maximum of 15, and LargeChange (which doubles as the visible portion of the scrolling range) is 3, then the possible return values go from 5 to 12.

So, to invert the value, you actually want to use:

scrollBar.Minimum + scrollBar.Maximum - scrollBar.LargeChange - scrollBar.Value

(Normally you can think of Value as position of the left or top edge of the thumb. The formula above will give you the bottom edge of the thumb. If you still want the top edge (ie values going from 8 to 15 in the example above), then use:

scrollBar.Minimum + scrollBar.Maximum - scrollBar.Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM