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