Novice trying to make a timer output to a label when a button is clicked.

Locked Novice trying to make a timer output to a label when a button is clicked.

  • Sunday, April 15, 2012 6:45 PM
     
     

    Hello, I've just started out in C# and decided to create a Runescape client and learn a little along the way, however, I can't seem to get my timer to output how long the user has been playing the game in a label. So far here's my code:

    Any suggestions?

    Thanks.

    public partial class ScapeRune : Form
        {
            public ScapeRune()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Timer timer = new Timer();
                try
                {
                    timer1.Enabled = false;
                    webBrowser1.Navigate("http://www.runescape.com/game.ws?j=1");
                    timer1.Enabled = true;
                    timer1.Start();
                    timer1.Interval = (1000) * (1);
                }
                catch { timer1.Enabled = true; }
                finally { timer1.Enabled = true; }
            }

             private void timer_Tick(object sender, EventArgs e)
            {
                label1.Text = timer1.ToString();
            }
        }
    }



    I also get this error: http://gyazo.com/fb53e562d9d3155f32c87f73dad7e783 

    Whilst compiling.

    Thanks guys.

           

All Replies

  • Sunday, April 15, 2012 7:28 PM
     
     
    If you have added timer1 and webBrowser1 using Form Editor, then you do not need the other timer variable. We suppose that timer_Tick was also added using Form Editor. (Actually the default name would be timer1_Click). These variables should not be null, unless you make some incorrect manual changes in generated code. In order to determine were the error occurs, try debugging the button1_Click in step-by-step mode. Check if timer1, webBrowser1 and label1 are not null. We hope you know how to use the Debugger.

    • Edited by Viorel_MVP Sunday, April 15, 2012 7:31 PM
    •  
  • Sunday, April 15, 2012 7:36 PM
     
     Answered Has Code

    You might want to try something along the lines of:

     public partial class ScapeRune : Form
     {
      private DateTime TimerStart;
      private Timer Timer1;
    
      public ScapeRune()
      {
       InitializeComponent();
    
       Timer1 = new Timer();
       Timer1.Interval = 100;
       Timer1.Tick += timer_Tick;
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
       Timer1.Start();
       TimerStart = DateTime.Now;
       webBrowser1.Navigate("http://www.runescape.com/game.ws?j=1");
      }
    
      private void timer_Tick(object sender, EventArgs e)
      {
       TimeSpan l_elapsedTime = new TimeSpan((DateTime.Now - TimerStart).Ticks);
       label1.Text = l_elapsedTime.ToString();
      }
    
     }
    Let me know if anything that I have presented is unclear.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


    • Edited by TSoftware Sunday, April 15, 2012 7:37 PM
    • Marked As Answer by BorsalinoKizaru Sunday, April 15, 2012 7:40 PM
    •  
  • Sunday, April 15, 2012 7:42 PM
     
     

    Thanks Tom,

    The code you displayed in your last post fixed the errors I was getting. Only problem is the timer in the label counts from milliseconds, seconds and so on, how would I just display hours, minutes and seconds?

    Thanks a lot.