I have a textbox control (Textbox1) and a label control lbl1. I have setup an x and y to capture the location of the control and then I use the mousemove/mouse down events to move the lbl1 control. The problem with this is I want the textbox1.value to change from 0 to 100 based on the mousemove x, so as I drag along the values in Textbox1 should increase from 0 to 100 as they would on a webpage slider. Is this possible?
'VBA script
Dim myX As Double
Dim myY As Double
Private Sub lbl1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
myX= X
myY= Y
End Sub
Private Sub Lbl1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button And 1 Then
lbl1.Left = lbl1.Left + (X - myX)
End If
If lbl1.Left < 6 Then
lbl1.Left = 6
ElseIf lbl1.Left > 126 Then
lbl1.Left = 126
End If
end sub
I have tried to use textbox1.value = lbl1.left -6 but this does not work and not sure of the calculation or if this is the correct way to change this value.
You need to do a little bit of math to scale that 6-126 to 0-100:
Dim myX As Double
Dim myY As Double
Private Sub lbl1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
myX = X
myY = Y
End Sub
Private Sub Lbl1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Const MIN_LEFT As Long = 6
Const MAX_SPAN As Long = 120
Dim newpos As Long, maxLeft As Long
If Button And 1 Then
maxLeft = MIN_LEFT + MAX_SPAN
newpos = lbl1.Left + (X - myX)
If newpos < MIN_LEFT Then newpos = MIN_LEFT
If newpos > maxLeft Then newpos = maxLeft
lbl1.Left = newpos
TextBox1.Text = 100 * (newpos - MIN_LEFT) / MAX_SPAN 'scale 6-126 value to 0-100
End If
End Sub