Click here to Skip to main content
13,762,666 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

22.4K views
3K downloads
69 bookmarked
Posted 17 May 2017
Licenced CPOL

C# Knob Control using Windows Forms

, 30 Aug 2018
Rate this:
Please Sign up or sign in to vote.
Yet another Knob Control in C#

Introduction

This code was originally written by Jigar Desai a long time ago as a proof of concept. (The original article for this control can be found on C-SharpCorner.com).

I needed a knob control in C#, with full functionalities for my personal projects and decided to improve it by making its output type a class library and adding as many properties as possible to be a full and flexible control.

  • The control accepts now graduations with text and sub graduations.
  • The text of the graduations can be drawn inside or outside the knob.
  • The "start angle" and the "end angle" of the graduations are adjustable.
    // "start angle" and "end angle" possible values:
    
    // 90 = bottom (minimum value for "start angle")
    // 180 = left
    // 270 = top
    // 360 = right
    // 450 = bottom again (maximum value for "end angle")
    
    // So the couple (90, 450) will give an entire circle 
    // and the couple (180, 360) will give half a circle.
  • The MouseWheel event is now managed.

Tip: You might have noticed that a UserControl doesn't show the MouseWheel event in the Properties window. Hint of trouble there. The WM_MOUSEWHEEL message bubbles. If the control that has the focus doesn't handle it, then Windows passes it on to its Parent. Repeatedly, until it finds a parent window that wants to handle it.

HandledMouseEventArgs lets you stop the bubbling.

protected override void OnMouseWheel(MouseEventArgs e)
{           
    base.OnMouseWheel(e);

    if ( isFocused && isKnobRotating && 
    Utility.isPointinRectangle(new Point(e.X, e.Y), rKnob))
    {                
        // the Delta value is always 120, as explained in MSDN
        int v = (e.Delta / 120) * (_maximum - _minimum) / _mouseWheelBarPartitions;
        SetProperValue(Value + v);

        // Avoid to send MouseWheel event to the parent container
        ((HandledMouseEventArgs)e).Handled = true;
    }
}

Properties available:

  • knobPointerStyle (enum): Sets the style of the knob pointer: a circle or a line
  • _minimum (int): Gets or sets the lower limit of the range of values
  • _maximum (int): Gets or sets the upper limit of the range of values
  • _LargeChange (int): Gets or sets a value to be added to or subtracted to the Value property when the change is made with the mouse.
  • _SmallChange (int): Gets or sets a value to be added to or subtracted to the Value property when the change is made with the keyboard.
  • _scaleDivisions (int): Sets the number of intervals between minimum and maximum
  • _scaleSubDivisions (int): Sets the number of subdivisions between main tick marks.
  • _scaleColor (Color): Color of tick marks
  • _knobBackColor (Color): backcolor of the knob (default LightGray)
  • _PointerColor (Color): backcolor of the pointer (default SlateBlue)
  • _drawDivInside (bool): Draws graduation strings inside or outside the knob circle
  • _showLargeScale (bool): Displays or hides the tick marks
  • _showSmallScale (bool): = Displays or hides intermediate tick marks
  • _startAngle = Sets the start angle to display graduations (default 135, min 90)
  • _endAngle = Sets the end angle to display graduations (default 405, max 450)
  • _mouseWheelBarPartitions = Sets how many parts are bar divided when using mouse wheel

The single event managed by this control is the ValueChanged event.

private void knobControl1_ValueChanged(object Sender)
{
     label1.Text = knobControl1.Value.ToString();
}

This project was developed with Visual Studio version 2017.

The code is not difficult to understand and to modify to suit your needs. The main difficulty is that you need some Math skills to understand how all is displayed (not so much in fact: sinus and cosinus level required :-)).

You can download the demo project to see the KnobControl in action or simply the source of the KnobControl.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fabrice Lacharme
Architect
France France
Architect/engineer

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
tbayart31-Aug-18 4:26
membertbayart31-Aug-18 4:26 
GeneralRe: My vote of 5 Pin
Fabrice Lacharme1-Sep-18 8:47
memberFabrice Lacharme1-Sep-18 8:47 
QuestionLicence missmatch Pin
DamianDixon30-Aug-18 22:59
memberDamianDixon30-Aug-18 22:59 
QuestionGood Pin
Yves23-Aug-18 10:37
memberYves23-Aug-18 10:37 
AnswerRe: Good Pin
Fabrice Lacharme25-Aug-18 0:34
memberFabrice Lacharme25-Aug-18 0:34 
QuestionThe true test... Pin
littleGreenDude22-Aug-18 5:30
memberlittleGreenDude22-Aug-18 5:30 
GeneralMy vote of 5 Pin
LightTempler22-Aug-18 4:20
memberLightTempler22-Aug-18 4:20 
BugNegative Minimum Doesn't work Pin
Member 419136627-Jul-18 4:16
memberMember 419136627-Jul-18 4:16 
GeneralRe: Negative Minimum Doesn't work Pin
Fabrice Lacharme21-Aug-18 22:18
memberFabrice Lacharme21-Aug-18 22:18 
QuestionScale Font Pin
हर्ष वर्द्धन27-May-18 6:00
memberहर्ष वर्द्धन27-May-18 6:00 
AnswerRe: Scale Font Pin
Fabrice Lacharme28-May-18 23:07
memberFabrice Lacharme28-May-18 23:07 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.181112.3 | Last Updated 30 Aug 2018
Article Copyright 2017 by Fabrice Lacharme
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid