Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please help me,
i would have a user input in textBox1 for my timer countdown,

What I have tried:

this is my code:


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;
using System.Diagnostics;

namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}


private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm : ss");
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}

private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click(sender, e);
}
}
}
Posted
Updated 24-Mar-16 1:23am
Comments
BillWoodruff 24-Mar-16 7:51am    
What is your goal here ? What does "user input in textBox1 mean" ?
Richard Deeming 24-Mar-16 9:08am    

1 solution

Um...that's some odd code you go there...
Look at what you are doing:
When your text changes, you pass the TextBox (as sender) to a handler that expects a button...which clears the textbox that causes the original event handler to be called, causing it to occur again...and stops the Timer. And since your Tick event changes the text in the textbox - causing the TextChanged event - all that will happen is (at best) a single Tick and a blank text box...

To be brutally honest, that code looks like you are guessing and hoping it will work - whatever it is you are trying to do and I can't begin to know from that code - and "hope and pray" doesn;t work in development. Stop.
Go back to the beginning, and decide what you want to do. Throw that code away, and look at what needs to happen in order for it work work as you want.
If you are trying to put a countdown (or count up) timer into a text box, that's trivial, but only if you think about what needs to happen instead of just trying things and hoping they will work!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900