# How to change radial gauge's value with the mouse/keyboard

## Solution

We will detect when the user either moves the mouse, keeping pressed the left mouse button or presses the up/down arrow keys. The below gif file illustrates the achieved behavior:

## Changing the Value with the Mouse

Handle the MouseMove event and check whether the left mouse button is pressed. Then, calculate the new gauge's value considering the angle of the current mouse location to the center of the gauge.

``````
{

}

{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
UpdateValue(e.Location);
}
}

private void UpdateValue(Point pointLocation)
{
PointF center = new PointF(centerX, centerY);

double radian = Math.Atan2(pointLocation.Y - center.Y, pointLocation.X - center.X);
double angle = radian * (180 / Math.PI);
if (angle < 0.0)
{
angle += 360.0;
}

}

public float CalculateValueByAngle(double needleAngleDegree, double rangeStart, double rangeEnd, double startAngle, double sweepAngle)
{
float value = 0;
double angleDiff = needleAngleDegree - startAngle;
if (angleDiff < 0)
{
angleDiff += 360;
}
double ratio = (angleDiff) / sweepAngle;
value = (float)(ratio * Math.Abs(rangeEnd - rangeStart) + rangeStart);
return value;
}

``````
``````Private Sub radRadialGauge1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
UpdateValue(e.Location)
End If
End Sub
Private Sub UpdateValue(ByVal pointLocation As Point)
Dim center As PointF = New PointF(centerX, centerY)
Dim radian As Double = Math.Atan2(pointLocation.Y - center.Y, pointLocation.X - center.X)
Dim angle As Double = radian * (180 / Math.PI)
If angle < 0.0 Then
angle += 360.0
End If
End Sub
Public Function CalculateValueByAngle(ByVal needleAngleDegree As Double, ByVal rangeStart As Double, ByVal rangeEnd As Double, ByVal startAngle As Double, ByVal sweepAngle As Double) As Single
Dim value As Single = 0
Dim angleDiff As Double = needleAngleDegree - startAngle
If angleDiff < 0 Then
angleDiff += 360
End If
Dim ratio As Double = (angleDiff) / sweepAngle
value = CSng((ratio * Math.Abs(rangeEnd - rangeStart) + rangeStart))
Return value
End Function

``````

## Changing the Value with the Keyboard

Add a timer to detect whether the up/down arrow key is pressed in order to adjust the value of RadRadialGauge. In the PreviewKeyDown event you can enable a flag to distinguish which arrow key is pressed. It is necessary to stop the timer when you release the mouse:

``````Private timer As Timer = New Timer()
Public Sub New()
InitializeComponent()
timer.Interval = 100
timer.Start()
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Dim newValue As Single
If isDownArrow Then
ElseIf isUpArrow Then
End If
End Sub
isDownArrow = False
isUpArrow = False
timer.[Stop]()
End Sub
Private isDownArrow As Boolean = False
Private isUpArrow As Boolean = False
If e.KeyData = Keys.Down Then
isDownArrow = True
ElseIf e.KeyData = Keys.Up Then
isUpArrow = True
End If
timer.Start()
End Sub

``````

A complete solution providing a C# and VB.NET project is available here.