gTrackBar - A Custom TrackBar UserControl (VB - NET) - CodeProject
gTrackBar - A Custom TrackBar UserControl (VB - NET) - CodeProject
NET) - CodeProject
Watch
TrackBar with custom coloring, value display, label, and increment buttons.
Is your email address OK? You are signed up for our newsletters but your email
address is either unconfirmed, or has not been reconfirmed in a long time. Please
click here to have a confirmation email sent so we can confirm your email address
and start sending you newsletters again. Alternatively, you can update your
subscriptions.
Download gTrackBar.zip
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 1/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
Introduction
I must have OCD (Obsessive Control Disorder). The standard TrackBar has annoyed me for a long
time, but I just put up with it until now. I finally got tired of the boring appearance, having to code
a TextBox or Label with it to display the value, and a Label for what it is. So, here is the TrackBar
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 2/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
I needed, hope it helps you too. There are too many properties to just list them out like I normally
would, so I decided to list the function of the property groups and a screenshot of the whole list.
1. The Control group contains the control's Border, Value, and Orientation properties.
2. FloatValue is the value that appears as the slider is moved with the mouse.
3. Label is the text that appears above the TrackBar.
4. Slider is the line, tick marks, and the slider button itself.
5. UpDownButtons are the buttons at each end of the TrackBar to increment the value by one.
6. ValueBox is the box displaying the value.
Properties
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 3/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
The Building Region contains the routines to setup the layout, size, and positions for all parts of the
control based on the properties set.
Painting
Override the Paint event to custom draw each piece of the control in the right place with the
correct orientation.
VB.NET Shrink ▲
MyBase.OnPaint(e)
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 4/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
End If
End Sub
I used basic GDI+ functions to draw each piece of the control. I will highlight some of the better
parts.
In DrawUpDnButtons, I basically have a button that needs to be drawn one of four ways depending
on the Orientation and which side of the control it is on. The button itself is just a rectangle with
a gradient color fill. The trick was the arrow. I could have calculated each arrow position and points
to create a separate GraphicsPath for each, but I used a Matrix instead to Rotate and/or
Translate a simple GraphicsPath to the right position.
First set three points and add a line between them to create the triangular arrow ^ shape:
VB
Dim gp As New GraphicsPath
Dim pts() As Point
Dim mx As New Matrix
For the left hand button, the GraphicsPath is oriented correctly so it just needs to be translated
(moved) to the right position inside the button's rectangle.
VB
With rectDownButton
mx.Translate(5, CSng((rectDownButton.Y _
+ (rectDownButton.Height / 2)) - 6))
gp.Transform(mx)
g.DrawPath(pn, gp)
End With
For the right hand button, the GraphicsPath needs to be flipped, but there isn't a built-in function
for flipping. Use this to flip the GraphicsPath horizontally instead: New Matrix(-1, 0, 0, 1,
image width here, 0).
VB.NET
With rectUpButton
mx = New Matrix(-1, 0, 0, 1, 5, 0)
mx.Translate(.X + 9, 0, MatrixOrder.Append)
gp.Transform(mx)
g.DrawPath(pn, gp)
End With
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 5/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
For the upper button, the GraphicsPath needs to be rotated 90 degrees. Find the center point and
RotateAt that point.
VB
With rectDownButton
mx.Translate(CSng((rectDownButton.X + _
(rectDownButton.Width / 2)) - 3), 4, MatrixOrder.Append)
gp.Transform(mx)
g.DrawPath(pn, gp)
End With
For the lower button, the GraphicsPath needs to be flipped again. Use this to flip the image
vertically: New Matrix(1, 0, 0, -1, 0, image height here).
VB.NET
With rectUpButton
mx = New Matrix(1, 0, 0, -1, 0, 10)
mx.Translate(0, .Y + 6, MatrixOrder.Append)
gp.Transform(mx)
g.DrawPath(pn, gp)
End With
A PointF type property without a custom TypeConverter: it works in code, but you cannot edit it
in the PropertyGrid.
With the PointFConverter, it performs just like the Point property in the PropertyGrid.
Create a property and add the TypeConverter attribute referencing the PointFConverter class we
are about to add.
VB
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 6/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
VB Shrink ▲
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 7/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
If (destinationType Is GetType(System.String) _
AndAlso TypeOf value Is PointF) Then
Sometimes with custom controls the properties can get a bit unwieldy. There are a lot of color
choices with this control and some of them have repeating patterns. For example, the Slider button
has three states and three properties (Face, Border, and Highlight). Instead of having nine colors
listed in a long line of confusing properties, they are grouped into three expandable properties
with three sub properties each. The ColorPack class and TypeConverter is used for the three slider
state properties. The ColorLinearGradient class and TypeConveter is used for the Linear gradient
properties of the slider lines. This makes the PropertyGrid look cleaner and easier to read.
Mouse Events
Here is where we check what part of the control the cursor is over and if the mouse button is
pressed. Based on this information, the Value is adjusted accordingly.
Because the MouseDown, Click and so on are a one time deal, a Timer is needed see if the mouse is
still down and if so change the value again. I didn't want it to run away as soon as it was clicked so
there is a built in delay right after the mouse is clicked, then after the delay it will begin
incrementing the value. The other issue is if the Min/Max span was big it would crawl super slow
and small spans would zip too fast, so the Timer's interval is adjusted based on how big the span
is.
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 8/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
VB
Private Sub MouseTimer_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MouseTimer.Tick
Key Events
I wanted to be able to adjust the Value by pressing the arrow keys. That sounded simple. I figured I
would just check the e.KeyValue in the KeyUp event and adjust the Value accordingly. Well, not so
simple. The problem is that the UserControl Inherits Button, which automatically handles the
arrow keys differently. After the KeyUp event, the focus jumps to the next control in the Tab Order,
even if you use e.Handled and e.SuppressKeyPress. I couldn't stop the focus change. Then, I
thought I would use the KeyDown event, but guess what, the arrows are automatically ignored
there. To fix this behavior, I override the IsInputKey function to allow the arrow keys. After this, the
focus will not jump away anymore.
VB Shrink ▲
OldValue = _Value
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 9/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
End Sub
History
Version 1.0 - March 2009
Version 1.2 - April 2009
Added Focus Rectangle
Separated the label into its own rectangle area for better layout and sizing
General Layout Fixes
License
This article, along with any associated source code and files, is licensed under The Code Project
Open License (CPOL)
Written By
SSDiver2112
Software Developer
United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved
to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba
buisness during this time. Currently I am a Database manager and software developer. I started
with VBA and VB6 and now having fun with VB.NET/WPF/C#...
Watch
Just wonderful
Filip D. Williams 16-Jul-21 2:17
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 11/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
NuGet package
antaveiv 1-Feb-19 23:50
THANK YOU!
Member 13796594 29-May-18 6:17
Silly Question
gianluca1 8-Dec-15 6:15
Up/Down Button
Member 11351552 1-Nov-15 6:44
Greetings friend
abascalufe 7-May-15 15:01
Just thanks
Member 11593747 9-Apr-15 19:57
No Scroll Event?
Member 8607614 12-Mar-15 6:47
My vote of 1
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 12/13
06/10/2022, 16:25 gTrackBar - A Custom TrackBar UserControl (VB.NET) - CodeProject
Re: My vote of 1
SSDiver2112 16-Mar-15 2:39
Range TrackBar
PDTPGY 31-Jul-14 19:46
Great!
Willy Kimura 26-Apr-14 14:54
My vote of 5
Mustapha Mahmoud 19-Mar-14 22:21
scroll event
Mustapha Mahmoud 14-Mar-14 9:24
Refresh 1 2 3 4 5 6 Next ᐅ
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
https://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET 13/13