CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 3rd, 2009, 04:36 PM
    italianboy italianboy is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    italianboy is an unknown quantity at this point (<10)
    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?
    Reply With Quote
      #2    
    Old November 3rd, 2009, 04:46 PM
    BigEd781 BigEd781 is offline
    Member +
     
    Join Date: Jun 2008
    Posts: 1,005
    BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)
    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.
    Reply With Quote
      #3    
    Old November 3rd, 2009, 04:59 PM
    italianboy italianboy is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    italianboy is an unknown quantity at this point (<10)
    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!
    Reply With Quote
      #4    
    Old November 3rd, 2009, 05:05 PM
    BigEd781 BigEd781 is offline
    Member +
     
    Join Date: Jun 2008
    Posts: 1,005
    BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)
    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( );
        }
    }
    Reply With Quote
      #5    
    Old November 3rd, 2009, 05:13 PM
    italianboy italianboy is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    italianboy is an unknown quantity at this point (<10)
    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'
    Reply With Quote
      #6    
    Old November 3rd, 2009, 05:26 PM
    BigEd781 BigEd781 is offline
    Member +
     
    Join Date: Jun 2008
    Posts: 1,005
    BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)
    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.
    Reply With Quote
      #7    
    Old November 3rd, 2009, 05:37 PM
    italianboy italianboy is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    italianboy is an unknown quantity at this point (<10)
    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!
    Reply With Quote
      #8    
    Old November 3rd, 2009, 05:40 PM
    BigEd781 BigEd781 is offline
    Member +
     
    Join Date: Jun 2008
    Posts: 1,005
    BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)
    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.
    Reply With Quote
      #9    
    Old November 3rd, 2009, 06:27 PM
    italianboy italianboy is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    italianboy is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #10    
    Old November 3rd, 2009, 06:50 PM
    BigEd781 BigEd781 is offline
    Member +
     
    Join Date: Jun 2008
    Posts: 1,005
    BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)BigEd781 is a glorious beacon of light (400+)
    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.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    button, label, timer
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:42 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009