CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2009
    Posts
    7

    Exclamation Face difficulties on timer

    hi,

    Firstly i click the start button then, the timer tick then the label will show up the time counted from 0s until the people solve the puzzle then the time will stop!

    How am i gonna write the coding i facing problem on that? I had no idea how am i gonna do it?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Face difficulties on timer

    Well, have you tried yet? It is pretty simple. The Timer class exposes an event called "Tick" which fires on every tick of the timer. So, set the timer duration, hook into the Tick event, start the timer. Now, in the Tick event handler method, update the label.

  3. #3
    Join Date
    Nov 2009
    Posts
    7

    Re: Face difficulties on timer

    Yea i already try many times b4 i post but i still cant mange to do that!
    I know is simple but its hard when come to the write code part!
    Can u gv me some link where can i reference to?
    Because i am a beginner in C# i am willing to learn it!

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Face difficulties on timer

    It's not too bad. Here is a sample off the top of my head:

    Code:
    class Form1 : Form
    {
        Timer _timer;
        int _tickCount;
    
        public Form1( )
        {
            _tickCount = 0;
            _timer = new Timer( );
            _timer.Interval = 1000; // ~1000 ms (1 second) between ticks
            _timer.Tick += timer_Tick;
            _timer.Start( );        
        }
    
        private void timer_Tick( object sender, EventArgs e )
        {
            label1.Text = ( _tickCount++ ).ToString( );
        }
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    7

    Re: Face difficulties on timer

    cant run its show error do i need to add something on it?
    timer_timer1;
    Error 1 'Timer' is a 'namespace' but is used like a 'type'

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Face difficulties on timer

    Quote Originally Posted by italianboy View Post
    cant run its show error do i need to add something on it?
    timer_timer1;
    Error 1 'Timer' is a 'namespace' but is used like a 'type'
    That would be a problem in your code. Why don't you post it so I can take a look. You should get used to handling errors like this so that you can learn the ins and outs of writing C# applications.

  7. #7
    Join Date
    Nov 2009
    Posts
    7

    Re: Face difficulties on timer

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Timer_2
    {
    public partial class Form1 : Form
    {
    Timer _timer;
    int _tickCount;


    public Form1()
    {
    _tickCount = 0;
    _timer = new Timer();
    _timer.Interval = 1000; // ~1000 ms (1 second) between ticks
    _timer.Tick += timer_Tick;
    _timer.Start();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    label1.Text = (_tickCount++).ToString();

    }
    }
    }


    But it appear error why? i do the thing like u mention on it!

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Face difficulties on timer

    You did not do it like I said. You have some minor errors, and a namespace with a name of "Timer_2" is kind of weird. Here is one problem:

    Code:
    _timer.Tick += timer_Tick;
    You are assigning a delegate method to the Tick event, namely, "timer_Tick". However, there is no such method because you have incorrectly declared the method as:

    Code:
    timer1_Tick
    The signatures do not match. I am not sure where the namespace collision is occurring, but for good measure, change the name of your namespace to something other than "Timer_2". Just right click it and refactor.

  9. #9
    Join Date
    Nov 2009
    Posts
    7

    Re: Face difficulties on timer

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace tutorial
    {
    public partial class Form1 : Form
    {
    Timer _timer;
    int _tickCount;

    public Form1()
    {
    _tickCount = 0;
    _timer = new Timer();
    _timer.Interval = 1000; // ~1000 ms (1 second) between ticks
    _timer.Tick += timer1_Tick;
    _timer.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
    label1.Text =(_tickCount++).ToString();

    }

    Now its show the error at label1.Text =(_tickCount++).ToString();
    Error code:Null references exception was unhandled
    Object reference not set to an instance of an object.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Face difficulties on timer

    ...You probably do not have a control named 'label1'. That was just an example.

    Honestly, I think you need to take a step back and spend some time learning the fundamentals.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured