|
|
Title | Use a PictureBox to make a slider with a value bar in Visual Basic .NET |
Description | This example shows how to use a PictureBox to make a slider with a value bar in Visual Basic .NET. |
Keywords | controls, PictureBox, slider, TrackBar, MouseDown, MouseMove, MouseUp, ToolTip, Visual Basic .NET, VB.NET |
Categories | Controls, 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 _
picSlider.Paint
' Calculate the needle's X coordinate.
Dim x As Single = ValueToX(SliderValue)
Dim y As Integer = CInt(picSlider.ClientSize.Height * _
0.25)
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.
|
|
|
|
|
|
|
|
|