Question Want to make a timer with user input

Shroomy

New member
Joined
Jul 14, 2017
Messages
1
Programming Experience
Beginner
Hello im just making a program for fun and to lean but im having trouble, what im trying to do is have a count down timer on a Form, i want the user to be able to set the time they want to count down from in this format hh:mm:ss (00:00:00), so far i have got a text box that counts down form 30, after I press the button, button1_click. Ive tried to make another text box and a button where the user can input the time there and press the button and it gets sent to the timer but hasn't worked. when you look at the code the time is stored in an int (int timeLeft;) so is it possible i could try to change that int value to what ever the user inputs.

C#:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        int timeLeft;


        public Form3()
        {
            InitializeComponent();
        }


        private void Form3_Load(object sender, EventArgs e)
        {


        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                // Display the new time left
                // by updating the Time Left label.
                timeLeft = timeLeft - 1;
                timeLabel.Text = timeLeft + " seconds";
            }
            else
            {
                // If the user ran out of time, stop the timer, show
                // a MessageBox, and fill in the answers.
                timer1.Stop();
                timeLabel.Text = "Time's up!";
                MessageBox.Show("You didn't finish in time.", "Sorry!");
                reset.Enabled = true;
            }


        }
        private void button1_Click(object sender, EventArgs e) //starts the count down
        {
            timeLeft = 30;
            timeLabel.Text = "30 seconds";
            timer1.Start();
        }


        private void button2_Click(object sender, EventArgs e)
        {
       ///    timeLabel.GetType();
     My attempt at imputing /////   MessageBox.Show(this.inputText.Text);








        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }
    }
}
 
I would probably do something like this (adapt to your needs and code as needed).

C#:
private DateTime m_stopTime;
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            if ( currentTime >= m_stopTime)
            {
                timer1.Stop();
            }
            else
            {
                TimeSpan span = m_stopTime.Subtract(currentTime);
                label1.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", span.Hours, span.Minutes, span.Seconds);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            m_stopTime = DateTime.Now + new TimeSpan(userHours, userMinutes, userSeconds);
            timer1.Start();
        }
 
Back
Top Bottom