Hi all!

Since I've been having a little more time for doing some recreational stuff I decided to continue programming again. But as you might have guessed from me posting here... I've found myself a problem again.

All around the internet as well as on tv you see these nifty counters. For example, one for counting how much money has been raised. The counter then shoots up from 0 to something like 10 million, and when coming close to the final number it slows down a bit and the slowly reaches the total number of raised funds.

I tried to create something similar with the simple code of:

int counter = 1500

for (int start = 0; start < counter; start++)
{
textbox1.Text = start;
}

But this goes WAY too fast!! When I click the button which activates this it waits like .1 second and then displays 1499 (easy to fix but I didn't care about the correctness of the number yet ;)).

So I tried looking up what needed to be done and started reading about timers and timespans and ticks and clicks and well... just about everything. But it didn't really make any sense to me.


So my first question would be: which part of timer do I use and how do I call upon that? (Ticks or Intervals, when do I use Start and Stop, things like that.)

Second, why does it seem so impossibly difficult to use a simple timer? Some examples of timers I've seen... Well, my Asteroids game has less code then that!

In short, any help regarding timers, be they links or solutions, would be greatly appreciated!


Thanks for reading!

Recommended Answers

All 2 Replies

Hi,
Timers its not what you are looking for becuase timers is to count time and you dont need it by what i understood.
What you need is:

System.Threading.Thread.Sleep(milliseconds);

To get time wait.

IdanS has a point here.
But I think if you want to use a Timer, start a new Forms app, drop a textbox on it and add the following:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int counter = 1;

        public Form1()
        {
            InitializeComponent();
         
            Timer MyTimer = new Timer();
            MyTimer.Interval = 1000; // about one second
            MyTimer.Tick += new EventHandler(MyTimer_Tick);
            MyTimer.Start(); // MyTimer.Enabled = true; is the same.
        }

        void MyTimer_Tick(object sender, EventArgs e)
        {
            Timer T = sender as Timer; //T is of course MyTimer here
            this.textBox1.Text = counter.ToString();
            counter++;
            T.Interval += 500; //increase interval with 0.5 sec
        }
    }
}

These are just some essentials, change them to your liking.
I don't think you can write an astroids game in less than about 10 lines of code:)

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.