C # / windows forms: binding trackbar and text field with coefficient

Linking the trackbar and text field is very simple in window forms. this is so: textBox.DataBindings.Add ("Text", trackBar, "Value");

the problem is that trackballs only allow integer values, but I want to have floating point values. therefore, I usually just divide the value by 100, since the value on the trackbar is not displayed directly to the user. but in the text box it is.

is it possible to relate these two factors to 100?

thanks!

+4
source share
2 answers

In the line of code that you added the Binding object to the DataBindings text box.

The Binding class has events called Format and Parse that you can use to perform division (the Format event takes a value from the trackbar and formats it for the text field) and multiplication (the Parse event takes a value from the text field and scales it for the track panel).

+4
source

You can use intermediate variables as shown below:

  public double v{set;get;} public int v100 { set { v = value / 100D; } get { return (int)(v* 100D); } } 

and dazzle them with the controls.

  trackBar.DataBindings.Add(new Binding("Value", PtParams, "v100")); textBox.DataBindings.Add(new Binding("Text", PtParams, "v")); 
+1
source

All Articles