I was doing a little playing around with some scrollbar controls and I noticed an odd behaviour that I was hoping someone could explain. I create three horizontal scrollbar controls, one for each primary colour. Possible values for each R, G or B are 0 to 255. When I set the min/max values to 0 and 255 it seems that the actual max I can get by sliding all the way to the right is 246. In order to allow a slideable value of 255, I have to set the maximum value to 264. This is definitely counter intuitive (or am I just having an extended "senior" moment).

Here is the code in full.

Public Class Form1

    Dim hsr(3) As System.Windows.Forms.HScrollBar

    Public Sub New()

        InitializeComponent()

        'Create three horizontal sliders, one for each R, G,and B colours
        'allowable values are from 0 to 255

        For i As Integer = 1 To 3

            hsr(i) = New System.Windows.Forms.HScrollBar
            hsr(i).Location = New System.Drawing.Point(30, 20 * i)
            hsr(i).Size = New System.Drawing.Size(300, 20)
            hsr(i).Minimum = 0
            hsr(i).Maximum = 264
            hsr(i).Value = 0
            hsr(i).SmallChange = 1
            hsr(i).Tag = i

            Me.Controls.Add(hsr(i))
            AddHandler hsr(i).ValueChanged, AddressOf ScrollColour

        Next

        SetColour()

    End Sub

    Private Sub SetColour()

        'set the bg colour depending on hscroll values

        Dim r As Integer = hsr(1).Value
        Dim g As Integer = hsr(2).Value
        Dim b As Integer = hsr(3).Value

        TextBox1.BackColor = Color.FromArgb(255, r, g, b)

    End Sub

    Private Sub ScrollColour(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim hsr As System.Windows.Forms.HScrollBar = sender

        SetColour()

        Select Case hsr.Tag
            Case 1 : lblR.Text = hsr.Value
            Case 2 : lblG.Text = hsr.Value
            Case 3 : lblB.Text = hsr.Value
        End Select

    End Sub

End Class

Edited 5 Years Ago by Reverend Jim: n/a

Comments
interesting topic :)

From the documentation on HScrollBar:
The maximum value can only be reached programmatically. The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached through user interaction is equal to 1 plus the Maximum property value minus the LargeChange property value. If necessary, you can set the Maximum property to the size of the object -1 to account for the term of 1.

If you set LargeChange to 1, you should be able to get to 255.

Edited 5 Years Ago by Momerath: n/a

Excellent. LargeChange = 1 did the trick. Thanks. I was looking through the installed documentation and couldn't find anything that explains what you did. I <b>did</b> find:

When the user presses the PAGE UP or PAGE DOWN key or clicks in the scroll bar track on either side of the scroll box, the Value property changes according to the value set in the LargeChange property.

When the user presses one of the arrow keys or clicks one of the scroll bar buttons, the Value property changes according to the value set in the SmallChange property.

When I tried to get more info I just get

The topic you requested could not be found in local help.

In spite of (supposedly) installing ALL of the documentation. I'm a bit of a dinosaur in that I prefer linear (ie book or ebook) documentation rather than documentation (ie Microsoft) which consists of a few lines of actual text and many more lines of links to "see also" topics. With a book you can read it all and know that you read it all. With pages and pages of links it is impossible to know if you missed something.

Edited 5 Years Ago by Reverend Jim: n/a

I do find it odd that they say you can only do it programmatically, when you can do it in the UI with this 'fix'. I'm guessing someone failed at testing :)

This question has already been answered. Start a new discussion instead.