Ejemplo en vb.net para crear un Slider usando controles de imagen
Código fuente ( controles : Pic_Barra, Pic_Slider )
Option Explicit On
Option Strict On
Public Class Form1
'Bandera para el evento MouseUp del picturebox
Dim bFlag As Boolean = False
' Valores máximos y mínimos del slider
Dim iValueMax As Integer
Dim iValueMin As Integer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
iValueMax = 100
iValueMin = 0
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic_slider.MouseDown
Dim Posx As Integer
Dim PosTempx As Integer
bFlag = True
While bFlag = True
'Cursor.Position.X devuelve la posición x del cursor en la pantalla
Posx = System.Windows.Forms.Cursor.Position.X
PosTempx = Posx ' temporal par a la pos x
Posx = Posx - (Me.Left + pic_slider.Width)
If (PosTempx <> Posx) And (Posx >= pic_Barra.Left) And _
(Posx + pic_slider.Width) <= (pic_Barra.Left + pic_Barra.Width) Then
'Cambia el Left
pic_slider.Left = Posx
' value
Dim iValor As Integer
' muestra el Value en el label
iValor = CInt((pic_slider.Left - pic_Barra.Left) * (iValueMax - iValueMin) / _
(pic_Barra.Width - pic_slider.Width)) + iValueMin
lblValue.Text = iValor.ToString
End If
' para no ocupar todos los recursos mientras se desliza el slider
Application.DoEvents()
End While
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic_slider.MouseUp
bFlag = False
' crear un tooltiptext
Dim ToolTip As New ToolTip()
With ToolTip
.IsBalloon = True ' tipo ballon
.ToolTipIcon = ToolTipIcon.Info ' icono
.ToolTipTitle = "Ejemplo ..." ' titulo
.InitialDelay = 1 'tiempo en aparecer
' texto
Dim sText As String = "Máximo: " & iValueMax.ToString & vbCrLf & _
"Mínimo: " & iValueMin.ToString & vbCrLf & _
"Valor: " & lblValue.Text.ToString
' Lo establece
.SetToolTip(pic_slider, sText)
End With
End Sub
End Class
Option Strict On
Public Class Form1
'Bandera para el evento MouseUp del picturebox
Dim bFlag As Boolean = False
' Valores máximos y mínimos del slider
Dim iValueMax As Integer
Dim iValueMin As Integer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
iValueMax = 100
iValueMin = 0
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic_slider.MouseDown
Dim Posx As Integer
Dim PosTempx As Integer
bFlag = True
While bFlag = True
'Cursor.Position.X devuelve la posición x del cursor en la pantalla
Posx = System.Windows.Forms.Cursor.Position.X
PosTempx = Posx ' temporal par a la pos x
Posx = Posx - (Me.Left + pic_slider.Width)
If (PosTempx <> Posx) And (Posx >= pic_Barra.Left) And _
(Posx + pic_slider.Width) <= (pic_Barra.Left + pic_Barra.Width) Then
'Cambia el Left
pic_slider.Left = Posx
' value
Dim iValor As Integer
' muestra el Value en el label
iValor = CInt((pic_slider.Left - pic_Barra.Left) * (iValueMax - iValueMin) / _
(pic_Barra.Width - pic_slider.Width)) + iValueMin
lblValue.Text = iValor.ToString
End If
' para no ocupar todos los recursos mientras se desliza el slider
Application.DoEvents()
End While
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic_slider.MouseUp
bFlag = False
' crear un tooltiptext
Dim ToolTip As New ToolTip()
With ToolTip
.IsBalloon = True ' tipo ballon
.ToolTipIcon = ToolTipIcon.Info ' icono
.ToolTipTitle = "Ejemplo ..." ' titulo
.InitialDelay = 1 'tiempo en aparecer
' texto
Dim sText As String = "Máximo: " & iValueMax.ToString & vbCrLf & _
"Mínimo: " & iValueMin.ToString & vbCrLf & _
"Valor: " & lblValue.Text.ToString
' Lo establece
.SetToolTip(pic_slider, sText)
End With
End Sub
End Class
Este comentario ha sido eliminado por un administrador del blog.
ResponderEliminar