How to create an advanced countdown timer

MattDAVM

Well, this question is related to this one, so you guys can understand it better

My Answer to it:

txtAtiv.Text = dataGridView1.Rows[0].Cells[1].Value + "";

string value = dataGridView1.Rows[0].Cells[2].Value + "";
lblLeft.Text = value.Split(' ')[1];
textStatus.Text = "";

DateTime timeConvert;
DateTime.TryParse(value, out timeConvert);

double time;
time = timeConvert.TimeOfDay.TotalMilliseconds;

var timeSpan = TimeSpan.FromMilliseconds(time);

lblSoma.Text = timeSpan.ToString();
timer2.Start();

According to the answer I wrote right there, I want to know if there's a way I can apply it to a timer and do the DataGrid values (converted) turn into a timer value. So if I press a button they start the countdown.

I have tried to insert this code inside the timer:

private void timer2_Tick(object sender, EventArgs e)
{
    string timeOp = dataGridView1.Rows[0].Cells[2].Value + "";
    DateTime timeConvert;
    DateTime dateTime = DateTime.Now;
    DateTime.TryParse(timeOp, out timeConvert);

    double time;
    time = timeConvert.TimeOfDay.TotalMilliseconds;
    var timeSpan = TimeSpan.FromMilliseconds(time);

    if (time > 0)
    {
        time = time - 1000; //(millisec)
        lblCountdown.text = time.ToString();
    }
}

didn't count down or anything, does someone has an idea of what should I do or why it isn't working?

DrewJordan

The value of time never changes, because you create it again fresh each time.

To solve this, you have to declare the variable you decrement outside of the Tick event.

Put these two variables on your form:

private int milliSecondsLeft = 0;
private bool timeSet = false;

Then change the 'tick' event to this:

private void timer2_Tick(object sender, EventArgs e)
{
    if (!timeSet) // only get the value once
    {
        string dateTimeFromGrid = "4/29/2016 5:00:00 AM"; //hardcoded for simplicity, get the string from your grid
        DateTime fromGrid;
        DateTime.TryParse(dateTimeFromGrid, out fromGrid);
        milliSecondsLeft = (int)fromGrid.TimeOfDay.TotalMilliseconds;  
        timeSet = true;
    }

    milliSecondsLeft = milliSecondsLeft - 100; // timer's default Interval is 100 milliseconds

    if (milliSecondsLeft > 0)
    {
        var span = new TimeSpan(0, 0, 0, 0, milliSecondsLeft);
        lblCountdown.Text = span.ToString(@"hh\:mm\:ss");
    }
    else
    {
        timer2.Stop();
    }
}

Make sure

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a "countdown timer" GIF?

From Dev

How to create a fade-in countdown timer?

From Dev

How to create countdown timer with animation on the View

From Java

How to create a simple countdown timer in Kotlin?

From Dev

How to create a countdown timer in a Java Applet?

From Dev

How to create a countdown timer in a Java Applet?

From Dev

Create a countdown timer on a event

From Dev

How to add a countdown timer

From Dev

Create Countdown Timer Based On String

From Dev

Create expired time, or countdown timer

From Dev

How to autostart a JavaScript Countdown Timer

From Dev

How can I countdown a timer

From Dev

how to countdown timer run in reactnative

From Dev

How to add text on countdown timer

From Dev

Create a countdown timer in javascript that starts countdown after 1 hr

From Dev

Create a countdown timer in javascript that starts countdown after 1 hr

From Dev

How do I create a countdown timer that persists when I browse to other pages of my app?

From Dev

How to create a countdown to Christmas

From Dev

How to make countdown timer with RxJS Observables?

From Dev

Swift: how to invalidate timer on animated SKShapenode countdown?

From Dev

How to make a countdown in angularjs with the timer directive

From Dev

Knockout js how to Implement Countdown Timer

From Dev

How to add a 10 second countdown timer to a popup

From Dev

How to use the chronometer as a countdown timer in Android

From Dev

how to implement Countdown timer with custom starting time?

From Dev

How to repeat countdown timer a certain number of times

From Dev

How do I display countdown timer in TextView?

From Dev

How to make countdown timer like in browser games?

From Dev

How to continue the countdown timer after closing ViewController

Related Related

  1. 1

    How to create a "countdown timer" GIF?

  2. 2

    How to create a fade-in countdown timer?

  3. 3

    How to create countdown timer with animation on the View

  4. 4

    How to create a simple countdown timer in Kotlin?

  5. 5

    How to create a countdown timer in a Java Applet?

  6. 6

    How to create a countdown timer in a Java Applet?

  7. 7

    Create a countdown timer on a event

  8. 8

    How to add a countdown timer

  9. 9

    Create Countdown Timer Based On String

  10. 10

    Create expired time, or countdown timer

  11. 11

    How to autostart a JavaScript Countdown Timer

  12. 12

    How can I countdown a timer

  13. 13

    how to countdown timer run in reactnative

  14. 14

    How to add text on countdown timer

  15. 15

    Create a countdown timer in javascript that starts countdown after 1 hr

  16. 16

    Create a countdown timer in javascript that starts countdown after 1 hr

  17. 17

    How do I create a countdown timer that persists when I browse to other pages of my app?

  18. 18

    How to create a countdown to Christmas

  19. 19

    How to make countdown timer with RxJS Observables?

  20. 20

    Swift: how to invalidate timer on animated SKShapenode countdown?

  21. 21

    How to make a countdown in angularjs with the timer directive

  22. 22

    Knockout js how to Implement Countdown Timer

  23. 23

    How to add a 10 second countdown timer to a popup

  24. 24

    How to use the chronometer as a countdown timer in Android

  25. 25

    how to implement Countdown timer with custom starting time?

  26. 26

    How to repeat countdown timer a certain number of times

  27. 27

    How do I display countdown timer in TextView?

  28. 28

    How to make countdown timer like in browser games?

  29. 29

    How to continue the countdown timer after closing ViewController

HotTag

Archive