How to input values through the keyboard

Input of values through the keyboard in SharpShooter Gauges

When working with gauges, you may want to add ability to enter some values using the keyboard (for example, through the TextBox). In this tutorial we will learn how to manage a gauge through TextBox.

Let’s create a simple WindowsForms application and place a gauge (in this sample we use the following gauge from SharpShooter Gauges: NewCollection -> Circles -> SparkleGreenSircle) and TextBox to enter values on the form.

 

To manage values it is required to complete the following steps:

  1. Get access to the required Slider element (in this sample, we need the Slider1 element).
  2. Subscribe for some event of the textBox1 to enable values input.

The sample code below demonstrates how it can be done:


public partial class Form1 : Form
{
    private Slider slider = null;
 
    public Form1()
    {
       InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        slider = widget1.Instrument.GetObject("Slider1") as Slider;
    }
 
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode != Keys.Enter)
        {
            return;
        }
 
        double value = 0;
        if(!double.TryParse(textBox1.Text, out value))
        {
            return;
        }
 
        slider.Value = value;
    }
}






Add Feedback