hey

just wondering how i can make a countdown timer in a windows forms app

or just how to display an updating value onto the form.

wanted to make a countdown for number of seconds the user inputs.

thnx.

Dani AI

Generated

For a Windows Forms countdown you should avoid the loop+Sleep approach used in console examples. ’s snippet is fine for CLI programs, but Sleep or long loops on the UI thread will freeze a Form. Use a timer that fires regularly to update a label (and keep the UI responsive), or run timing on a background thread and marshal updates to the UI.

Typical pattern (quick checklist): place a numeric input for seconds, a Label for the remaining time, and a Start button. Use System.Windows.Forms.Timer with Interval = 1000 for simple second updates. On Tick compute remaining time and update the Label. For better accuracy avoid accumulating decrements — record an initial TimeSpan and a Stopwatch, then compute remaining = initial - stopwatch.Elapsed each Tick to prevent drift. If using System.Timers.Timer or Threading.Timer, call Control.Invoke/BeginInvoke to update controls.

Example (C# WinForms: use this inside the Form class):

private System.Windows.Forms.Timer uiTimer = new System.Windows.Forms.Timer();
private System.Diagnostics.Stopwatch sw;
private TimeSpan countdown;

public Form1()
{
    InitializeComponent();
    uiTimer.Interval = 1000;
    uiTimer.Tick += UiTimer_Tick;
}

private void btnStart_Click(object sender, EventArgs e)
{
    countdown = TimeSpan.FromSeconds((double)numericUpDownSeconds.Value);
    sw = System.Diagnostics.Stopwatch.StartNew();
    uiTimer.Start();
    btnStart.Enabled = false;
}

private void UiTimer_Tick(object sender, EventArgs e)
{
    var remaining = countdown - sw.Elapsed;
    if (remaining <= TimeSpan.Zero)
    {
        uiTimer.Stop();
        sw.Stop();
        labelRemaining.Text = "0s";
        btnStart.Enabled = true;
        return;
    }
    labelRemaining.Text = string.Format("{0:%h}h {0:%m}m {0:%s}s", remaining);
}

Troubleshooting notes: dispose or stop the timer on FormClosing; validate input; avoid updating UI from non-UI threads without Invoke; for native C++ use SetTimer/WM_TIMER or higher-resolution multimedia timers if you need sub-second precision; C++/CLI WinForms follows the same .NET pattern. ’s acknowledgment of is noted — the console approach demonstrates the logic, but the UI pattern above is the WinForms way.

Recommended Answers

All 2 Replies

You should try to search the forum before asking for help.

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>


using namespace std;

int main()

{

int m, s,h;

cout << "A COUNTDOWN TIMER " << endl;
cout << "enter time in hours here" << endl;
cin >> h;
cout << "enter time in minutes here " << endl;
cin >> m;

cout << "enter im in seconds here" << endl;
cin >> s;

cout << "Press any key to start" << endl;

cout << " A COUNTDOWN TIMER" << endl;
cout << "time remaining" << endl;
cout << "hours : " << h << "mins : " << m << " secs : " << s << endl;

for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0 ; min--)
{
if ( min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if ( sec == 0 )
s = 59;
Sleep(1000);
system("cls");
cout << hour << " :hours " << min << " :mins " << sec << " :secs" << endl;
}
}
}

Sleep(1000);
cout << "THE END" << endl;


return 0;

:thank u very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.