-
November 3rd, 2009, 04:36 PM
#1
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?
-
November 3rd, 2009, 04:46 PM
#2
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.
-
November 3rd, 2009, 04:59 PM
#3
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!
-
November 3rd, 2009, 05:05 PM
#4
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( );
}
}
-
November 3rd, 2009, 05:13 PM
#5
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'
-
November 3rd, 2009, 05:26 PM
#6
Re: Face difficulties on timer
Originally Posted by italianboy
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.
-
November 3rd, 2009, 05:37 PM
#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!
-
November 3rd, 2009, 05:40 PM
#8
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:
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.
-
November 3rd, 2009, 06:27 PM
#9
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.
-
November 3rd, 2009, 06:50 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|