Sefa DEDE
Sefa DEDE

Reputation: 5

Changing date and time with trackbar

Currently, I'm working on a C# sample app with Windows Forms and I want to be able to change date with slider / trackbar. Actually, I did that example with integer values to converting string however couldn't find how to apply with DateTime object. Any clue / library or doc. would be great.

Sample code:

public partial class SampleForm : Form
{
    public SampleForm()
    {
        InitializeComponent();
        label1.text = "0";
    }

    private void Form_Load() { /* tracbar settings... */ }

    private void trackBar_Scroll(object sender, EventArgs e)
    {
        label1.text; // trackbar.Value.ToString()
    }

    private void trackBar_ValueChanged(object sender, EventArgs e)
    {
        // changing date and time while trackbar process
    }
}

Upvotes: 0

Views: 410

Answers (1)

Luchspeter
Luchspeter

Reputation: 804

Using the DateTime class and the AddDays method you can do what you want to do:

public SampleForm()
{
    InitializeComponent();
    label1.text = "0";
    StartDate = DateTime.Now;
}
DateTime StartDate;
private void trackBar_ValueChanged(object sender, EventArgs e)
{
   label1.Text = StartDate.AddDays(trackbar.Value).ToShortDateString();
}

Of course, you might have to map your trackbar values to another fitting range.

Upvotes: 0

Related Questions