trackbar and double values

  • Thread starter Thread starter martins
  • Start date Start date
M

martins

hi

how can I configure/set the trackbar control in order to scroll between
decimal values

i.e.

between 0.5 and 1

for example

How can I do this ?
 
As you've probably noticed the TrackBar only supports int types. But
you could write a control that inherits from TrackBar and then create
NEW properties returning decimal types - using the new keyword with the
calculations factored by 0.1.

Here's an attempt (setting the Factor property adjusts the values that
are stored in the underlying control) :

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

namespace TrackBarDecimals
{
public class MyTrackBar : System.Windows.Forms.TrackBar
{

private System.ComponentModel.Container components = null;

public MyTrackBar()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void RecalcValue()
{
Value = (decimal)(base.Value * factor);
}

protected override void OnValueChanged(EventArgs e)
{
base.OnValueChanged(e);
RecalcValue();
}

protected override void OnScroll(EventArgs e)
{
base.OnScroll(e);
RecalcValue();
}


[Browsable(true)]
public new decimal Maximum
{
get { return (decimal)(base.Maximum * factor); }
set { base.Maximum = (int)(value / factor); }
}


[Browsable(true)]
public new decimal Minimum
{
get { return (decimal)(base.Minimum * factor); }
set { base.Minimum = (int)(value / factor); }
}


private decimal factor = 0.1M;
[Browsable(true)]
public decimal Factor
{
get { return factor;}
set { factor = value;}
}

private decimal myValue;
[Browsable(true)]
public new decimal Value
{
get { return myValue; }
set { myValue = value; }
}

[Browsable(true)]
public new decimal TickFrequency
{
get { return (decimal)(base.TickFrequency * factor); }
set { base.TickFrequency = (int)(value / factor); }
}

[Browsable(true)]
public new decimal SmallChange
{
get { return (decimal)(base.SmallChange * factor); }
set { base.SmallChange = (int)(value / factor); }
}

[Browsable(true)]
public new decimal LargeChange
{
get { return (decimal)(base.LargeChange * factor); }
set { base.LargeChange = (int)(value / factor); }
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

Here's a test form that uses it:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TrackBarDecimals
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private TrackBarDecimals.MyTrackBar myTrackBar1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.myTrackBar1 = new TrackBarDecimals.MyTrackBar();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.myTrackBar1)).BeginInit();
this.SuspendLayout();
//
// myTrackBar1
//
this.myTrackBar1.Factor = new System.Decimal(new int[] {
1,
0,
0,
65536});
this.myTrackBar1.LargeChange = new System.Decimal(new int[] {
5,
0,
0,
65536});
this.myTrackBar1.Location = new System.Drawing.Point(32, 32);
this.myTrackBar1.Maximum = new System.Decimal(new int[] {
10,
0,
0,
65536});
this.myTrackBar1.Minimum = new System.Decimal(new int[] {
5,
0,
0,
65536});
this.myTrackBar1.Name = "myTrackBar1";
this.myTrackBar1.Size = new System.Drawing.Size(384, 42);
this.myTrackBar1.SmallChange = new System.Decimal(new int[] {
1,
0,
0,
65536});
this.myTrackBar1.TabIndex = 0;
this.myTrackBar1.TickFrequency = new System.Decimal(new int[] {
1,
0,
0,
65536});
this.myTrackBar1.Value = new System.Decimal(new int[] {
5,
0,
0,
65536});
this.myTrackBar1.ValueChanged += new
System.EventHandler(this.myTrackBar1_ValueChanged);
this.myTrackBar1.Scroll += new
System.EventHandler(this.myTrackBar1_Scroll);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 96);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 157);
this.Controls.Add(this.label1);
this.Controls.Add(this.myTrackBar1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.myTrackBar1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void myTrackBar1_Scroll(object sender, System.EventArgs e)
{
label1.Text = "Scroll Value=" + myTrackBar1.Value;
}

private void myTrackBar1_ValueChanged(object sender, System.EventArgs
e)
{
label1.Text = "Val=" + myTrackBar1.Value;
}
}
}

It's kind of there :-)
 
Back
Top