How to use floats with TrackBar

后端 未结 2 1536
我寻月下人不归
我寻月下人不归 2021-01-05 04:27

I\'m using a TrackBar control. By default its values are int32. I would really like to use decimal values so the use can select at a more granular

2条回答
  •  遥遥无期
    2021-01-05 05:03

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

    The following works 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); } }
    }
    

提交回复
热议问题