C# Knob Control using Windows Forms

0
25

Introduction

Download source (117k)

Download demo project (218k)

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 drawned inside or outside the knob.
  • The “start angle” and the “end angle” of the graduations are ajustable. 

 

  • 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))
    {                
            int v = (e.Delta / 120) * (_maximum - _minimum) / _mouseWheelBarPartitions;
        SetProperValue(Value + v);

                ((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 is 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 developped 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.

LEAVE A REPLY