locked
Custom Trackbar RRS feed

  • Question

  • Hi,

    I want to create a custom trackbar. I google'd around and saw this codes then modified it with my likings. One thing I noticed is that as I change my VALUE on the DESIGN VIEW of VS2008 the slider of the Trackbar doesn't update to its changes / move to the value that I inputted. It inherits the Forms.Trackbar Properties so I think I do not need to overrride the ValueChanged Events since I only want on my Trackbar is to allow Double/Decimal Values. I've tried also setting up the value upon the Form Load still no luck.

    Below are the codes:
     
    public partial class TrackBarDecimals : TrackBar {
    
            private decimal factor = 0.10M;
    
            private decimal _value = 4;
    
            private decimal _minimum = 0;
    
            private decimal _maximum = 10;
    
            private decimal _largeChange = 2;
    
            private decimal _smallChange = 1;
    
            
    
            private decimal _tickFrequency = 1;
    
    
    
            public event EventHandler ValueChanged;
    
    
    
    
    
            public TrackBarDecimals ()
    
            {
    
                InitializeComponent ();
    
            }
    
    
    
            private void RecalcValue ()
    
            {
    
                Value = (decimal) (Value * factor);
    
            }
    
    
    
            protected override void OnValueChanged (EventArgs e)
    
            {
    
                base.OnValueChanged (e);
    
                RecalcValue ();
    
    
    
                this.Invalidate ();
    
            }
    
    
    
            protected override void OnScroll (EventArgs e)
    
            {
    
                base.OnScroll (e);
    
                RecalcValue ();
    
                this.Invalidate ();
    
            }
    
           
    
            [Browsable (true)]
    
            public new decimal Maximum
    
            {
    
                get
    
                {
    
                    return (decimal) (_maximum * factor);
    
                }
    
                set
    
                {
    
    
    
                    _maximum = (int) (value / factor);
    
    
    
                    if (_maximum < _value)
    
                        _value = _maximum;
    
                    if (_maximum < _minimum)
    
                        _minimum = _maximum;
    
                    this.Invalidate ();
    
                }
    
            }
    
           
    
            [Browsable (true)]
    
            public new decimal Minimum
    
            {
    
                get
    
                {
    
                    return (decimal) (_minimum * factor);
    
                }
    
                set
    
                {
    
                    _minimum = (int) (value / factor);
    
    
    
                    if (_minimum > _maximum)
    
                        _maximum = _minimum;
    
                    if (_minimum > _value)
    
                        _value = _minimum;
    
    
    
                    this.Invalidate ();
    
                }
    
            }
    
    
    
            [Browsable (true)]
    
            public decimal Factor
    
            {
    
                get
    
                {
    
                    return factor;
    
                }
    
                set
    
                {
    
                    factor = value;
    
                    this.Invalidate ();
    
                }
    
            }
    
          
    
            [Browsable (true)]
    
            public new decimal Value
    
            {
    
                get
    
                {
    
                    return _value;
    
                }
    
                set
    
                {
    
                    if (_value != value)
    
                    {
    
                        if (value < _minimum)
    
                            _value = _minimum;
    
                        else
    
                            if (value > _maximum)
    
                                _value = _maximum;
    
                            else
    
                                _value = value;
    
                    }
    
                    
    
                    this.Invalidate ();
    
                }
    
            }
    
    
    
            [Browsable (true)]
    
            public new decimal TickFrequency
    
            {
    
                get 
    
                { 
    
                    return (decimal) (_tickFrequency * factor); 
    
                }
    
                set
    
                {
    
                    if (_tickFrequency != value)
    
                    {
    
                        _tickFrequency = (int) (value / factor);
    
                        this.Invalidate ();
    
                    }
    
                    this.Invalidate ();
    
                }
    
            }
    
    
    
            [Browsable (true)]
    
            public new decimal SmallChange
    
            {
    
                get 
    
                { 
    
                    return (decimal) (_smallChange * factor); 
    
                }
    
                set
    
                {
    
                    _smallChange = (int) (value / factor);
    
                    this.Invalidate ();
    
                }
    
            }
    
        
    
            [Browsable (true)]
    
            public new decimal LargeChange
    
            {
    
                get 
    
                { 
    
                    return (decimal) (_largeChange * factor); 
    
                }
    
                set
    
                {
    
                    _largeChange = (int) (value / factor);
    
                    this.Invalidate ();
    
                }
    
            }
    
        }
    
    

    • Edited by gnaungayan Thursday, May 28, 2009 11:51 AM
    Thursday, May 28, 2009 1:23 AM

Answers

  • using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class TrackBarDecimal : TrackBar {
      private decimal mValue;
      private decimal mFactor = 0.1M;

      public decimal Factor {
        get { return mFactor; }
        set { mFactor = value; base.Value = (int)(mValue / mFactor); }
      }
      public new decimal Value {
        get { return mValue; }
        set { mValue = value; base.Value = (int)(mValue / mFactor); }
      }
    }
    Hans Passant.
    • Marked as answer by gnaungayan Thursday, May 28, 2009 2:59 PM
    Thursday, May 28, 2009 1:24 PM

All replies

  • There are a lot of problems with this code.  The chief one being that you override the Value property but don't update base.Value.  That's why the pointer doesn't move.  RecalcValue has to use base.Value.

    Hans Passant.
    Thursday, May 28, 2009 12:04 PM
  • I've also tried removing all override methods and using new modifier for certain properties thus replicating a Trackbar. I've also removed the Factor and RecalcValue still pointer doesn't move. How would I fix this?
    Thursday, May 28, 2009 1:06 PM
  • using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class TrackBarDecimal : TrackBar {
      private decimal mValue;
      private decimal mFactor = 0.1M;

      public decimal Factor {
        get { return mFactor; }
        set { mFactor = value; base.Value = (int)(mValue / mFactor); }
      }
      public new decimal Value {
        get { return mValue; }
        set { mValue = value; base.Value = (int)(mValue / mFactor); }
      }
    }
    Hans Passant.
    • Marked as answer by gnaungayan Thursday, May 28, 2009 2:59 PM
    Thursday, May 28, 2009 1:24 PM
  • Thanks man works perfectly. By the way you said Recalc Value has to used base.value do you mean its like this

    Value = base.value * mFactor?
    Thursday, May 28, 2009 2:03 PM