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 ();
}
}
}