[ACCEPTED]-How to use floats with TrackBar-controls

Accepted answer
Score: 22

You can use a multiplier. Say, for example, you 11 wanted to make your TrackBar control go 10 from 0 - 5 with 0.01 increments. Just set 9 the Minimum to 0, the Maximum to 500, and 8 increment by 1.

When you go to set your float 7 value, multiply it by 100, and use that 6 for the TrackBar value.

You should be able 5 to get any (realistic) degree of precision 4 in this manner, since the TrackBar works 3 with ints, and has the full data range of 2 Int32 available. This is much more precision 1 than a user interface requires.

Score: 6

Another idea would be to inherit from TrackBar and simulate 4 float values in a custom class in the way 3 Reed Copsey suggested to use ints and multiply 2 with a precision factor.

The following works 1 pretty neat for small float values:

class FloatTrackBar: TrackBar
{
    private float precision = 0.01f;

    public float Precision
    {
        get { return precision; }
        set
        {
            precision = value;
            // todo: update the 5 properties below
        }
    }
    public new float LargeChange
    { get { return base.LargeChange * precision; } set { base.LargeChange = (int) (value / precision); } }
    public new float Maximum
    { get { return base.Maximum * precision; } set { base.Maximum = (int) (value / precision); } }
    public new float Minimum
    { get { return base.Minimum * precision; } set { base.Minimum = (int) (value / precision); } }
    public new float SmallChange
    { get { return base.SmallChange * precision; } set { base.SmallChange = (int) (value / precision); } }
    public new float Value
    { get { return base.Value * precision; } set { base.Value = (int) (value / precision); } }
}

More Related questions