What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse a PictureBox to make a slider with a value bar in Visual Basic .NET
DescriptionThis example shows how to use a PictureBox to make a slider with a value bar in Visual Basic .NET.
Keywordscontrols, PictureBox, slider, TrackBar, MouseDown, MouseMove, MouseUp, ToolTip, Visual Basic .NET, VB.NET
CategoriesControls, Controls, Controls

The example Use a PictureBox to make a slider with a needle in Visual Basic .NET shows how to use a PictureBox to make a simple slider. This example is similar except it draws the slider in a different style. See the previous example for information about how to make the slider.

The following code shows the only difference between this example and the previous one: the way it draws.

' Draw the needle.
Private Sub picSlider_Paint(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles _
    ' Calculate the needle's X coordinate.
    Dim x As Single = ValueToX(SliderValue)
    Dim y As Integer = CInt(picSlider.ClientSize.Height * _
    Dim hgt As Integer = picSlider.ClientSize.Height - 2 * y

    ' Draw it.
    e.Graphics.FillRectangle(Brushes.Blue, _
        0, y, x, hgt)
    using pen as new Pen(Color.Blue, 3)
        e.Graphics.DrawLine(pen, _
            x, 0, _
            x, picSlider.ClientSize.Height)
    End Using
End Sub
This code draws a thin bar horizontally inside the PictureBox. It then draws a vertical line at the selected value.

You can modify the Paint event handler further to produce other effects. For example, you could draw the area to the left of the value in a solid color, fill it with a gradient brush, or tile the area with a picture.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.