Click here to Skip to main content
16,020,822 members
Articles / Programming Languages / Visual Basic
Article

VB.NET Knob Control using Windows Forms and GDI+

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
11 Jul 2002 151K   4.6K   40   13
User Drawn Control for .NET Winforms using GDI+

Knob Control Image

This code was originally written by Jigar Desai using C#. Naturally, as a VB Programmer, I needed to see if this control could be readily implemented using VB.NET. After many hours of hair pulling, this control has been successfully ported to VB.NET . Additionally, I added a new design-time property to the original control which allows the Knob Color to take on a different color than the control's background. The only change to the original code is the addition of the "KnobColor" Property. I have included the "TestKnobControl" Project to demonstate the use of this control's ValueChanged Event as well as the vast array of different colors now available for the control.

This control implements several interesting techniques, to include: Delegates, Properties, GDI+ and Overriding base methods. Special thanks, from the VB Community, to Jigar for his quick and positive response on my request to post this control in VB.NET.

The original Article for this control can be found on C-SharpCorner.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionActiveX version Pin
Member 806659323-Nov-14 9:12
Member 806659323-Nov-14 9:12 
GeneralTick Marks fix Pin
User 431396922-Sep-07 16:05
User 431396922-Sep-07 16:05 
GeneralModification Pin
m1598118-Nov-06 12:59
m1598118-Nov-06 12:59 
GeneralVB.NET 2005 Pin
ultra029-Apr-06 17:11
ultra029-Apr-06 17:11 
GeneralUpdate for some Value visualization.. Pin
woo_hoo15-Nov-05 20:31
woo_hoo15-Nov-05 20:31 
QuestionC# version ? Pin
John H Long3-Dec-03 4:33
John H Long3-Dec-03 4:33 
AnswerRe: C# version ? Pin
blong4-Dec-03 13:59
blong4-Dec-03 13:59 
GeneralRe: C# version ? Pin
John H Long7-Dec-03 3:18
John H Long7-Dec-03 3:18 
GeneralRe: C# version ? Pin
Michal Brylka8-Feb-07 7:30
Michal Brylka8-Feb-07 7:30 
GeneralBug... Pin
Francois Germain17-Feb-03 16:33
Francois Germain17-Feb-03 16:33 
GeneralRe: Bug... Pin
blong18-Feb-03 9:58
blong18-Feb-03 9:58 
GeneralRe: Bug... A fix of the cause of the error Pin
tntiger26-Sep-03 7:59
tntiger26-Sep-03 7:59 
The problem seems to be a divide-by-zero problem.
Below, the original function accounted for the case of
p.X <= pKnob.X with degree = (pKnob.Y - p.Y) / (pKnob.X - p.X).
The problem is when p.X = pKnob.X then that reduces to (pKnob.Y - p.Y) / 0.
Here is part of the original code. Fix follows below that.

Private Function getValueFromPosition(ByVal p As Point) As Integer
Dim degree As Double = 0.0
Dim v As Integer = 0
If (p.X <= pKnob.X) Then
degree = (pKnob.Y - p.Y) / (pKnob.X - p.X)
degree = Math.Atan(degree)
degree = (degree) * (180 / Math.PI) + 45
v = (degree * (Me.Maximum - Me.Minimum) / 270)
ElseIf (p.X > pKnob.X) Then
degree = (p.Y - pKnob.Y) / (p.X - pKnob.X)
degree = Math.Atan(degree)
degree = 225 + (degree) * (180 / Math.PI)
v = (degree * (Me.Maximum - Me.Minimum) / 270)
End If

If (v > Maximum) Then v = Maximum
If (v < Minimum) Then v = Minimum
Return v
End Function

The fix:
Since p.X = pKnob.X when the mouse is exactly half way horizontally across
the knob. It seems only fair to make the value returned half the range of
the knob's scale. Even in the no-go zone at the bottom of the knob, the
original version still snaps to full scale or minimum as you move across the
bottom area. The adjusted function below snaps the value to half scale right
on the middle pixel of the control and avoids the divide-by-zero problem.


Private Function getValueFromPosition(ByVal p As Point) As Integer
Dim degree As Double = 0.0
Dim v As Integer = 0
If (p.X < pKnob.X) Then
degree = (pKnob.Y - p.Y) / (pKnob.X - p.X)
degree = Math.Atan(degree)
degree = (degree) * (180 / Math.PI) + 45
v = (degree * (Me.Maximum - Me.Minimum) / 270)
ElseIf (p.X > pKnob.X) Then
degree = (p.Y - pKnob.Y) / (p.X - pKnob.X)
degree = Math.Atan(degree)
degree = 225 + (degree) * (180 / Math.PI)
v = (degree * (Me.Maximum - Me.Minimum) / 270)
ElseIf (p.X = pKnob.X) Then
v = (Me.Maximum + Me.Minimum) / 2
End If

If (v > Maximum) Then v = Maximum
If (v < Minimum) Then v = Minimum
Return v
End Function

To fix, open the KnobContol.vb code and alter as above. Rebuild KnobControl,
NOT the solution, then Close the project. To see the effect, open up
TestKnobControl and in the solution Explorer, click the References node to
expand it. Right-click on KnobControl, click Delete, to remove the
reference. Right-click References and select Add Reference. In the Add
Reference window that opens, click Browse and navigate to where the
KnobControl project is and go to the bin directory under it. Select
KnobControl.dll and click Open. Back in the Add Reference window, click OK.
Now run the updated TestKnobControl project.


Mitchell Day
mitchell_day@hotmail.com
GeneralA couple of questions... Pin
Josh Martin12-Jul-02 13:31
Josh Martin12-Jul-02 13:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.