5 Replies - 2776 Views - Last Post: 27 October 2010 - 07:02 AM Rate Topic: -----

#1 jobyjames85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-October 10

Timer Tick

Posted 27 October 2010 - 05:03 AM

I'm trying to create a simple animation, where i display Numbers one after another .but i get the last number "10"


I used this code but it show last number

private void button1_Click(object sender, EventArgs e)
	        {
	            timer1.Enabled = true;
	        }
	 
	private void timer1_Tick(object sender, EventArgs e)
	        {
	 
	            for (int j = 0; j <= 10; j++)
	            {
	                label.text = i.Tostring();
	            }

                 }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 27 October 2010 - 05:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Timer Tick

#2 PhunkRabbit  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 406
  • Joined: 23-August 10

Re: Timer Tick

Posted 27 October 2010 - 05:08 AM

Please use code tags, [code ] [/code ] - without the spaces.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Timer Tick

Posted 27 October 2010 - 05:11 AM

What is your timer tick interval set to?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Timer Tick

Posted 27 October 2010 - 06:00 AM

if you didn't change the Interval of the Timer, then it's set to 100 milliseconds. Since you are only going to 10, it will take the application 1 second to complete loop through the for loop. All you will see is the last number.
Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4524
  • View blog
  • Posts: 7,892
  • Joined: 08-June 10

Re: Timer Tick

Posted 27 October 2010 - 06:51 AM

You have to understand how a timer works. That loop in the "Tick" method is not going to execute one iteration per tick. What's going to happen is, your program will wait until the timer ticks once, then immediately execute all the code in the "Tick" method. Meaning that loop will happen literally as fast as the computer can process it. Faster than the UI will update it, actually, since it's happening on the same thread. So the label will simply change to the last value of the loop.

What you'll need to do is have a global variable that counts "ticks." Each time the timer ticks, add one to that variable and restart the timer. Also have a variable that holds a "max value." When the ticks == max value, stop restarting the timer.
Was This Post Helpful? 1
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Timer Tick

Posted 27 October 2010 - 07:02 AM

NOT TESTED!

//Save the number you're on! :D/>
int WhatNumberAmIOn = 0;

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

private void timer1_Tick(object sender, EventArgs e)
{
    if (WhatNumberAmIOn <= 10)
    {
        WhatNumberAmIOn++; //Add 1 each time your timer ticks!
        label.Text = WhatNumberAmIOn;
    }
    else
    {
        timer1.Enabled = false;
    }
}



Just have a variable keep track of what's going on and you're set.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1