Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strange problem with display time

Strange problem with display time

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andredani
    wrote on last edited by
    #1

    I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!

    R Z 2 Replies Last reply
    0
    • A andredani

      I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!

      R Offline
      R Offline
      Rei Miyasaka
      wrote on last edited by
      #2

      Almost no textbook or college course teaches you to use a debugger. But you should. Try selecting the first line in myTimer_Tick(), hit F9 to set a breakpoint, then run the program. When it stops there, put your mouse over the variables or look at the Watch box to see what the values are on the variables. Then execute one line at a time (Debug -> Step Over) and figure out what's wrong. It's a really good way to fix problems and learn programming. By the way, what exactly are you trying to do?

      1 Reply Last reply
      0
      • A andredani

        I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!

        Z Offline
        Z Offline
        zafersavas
        wrote on last edited by
        #3

        First of all : Avoid declaring many variables. So Instead of void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } You can just use which is more readable. void myTimer_Tick(object sender, EventArgs e) { label21.Text = (Convert.ToInt32(label21.Text) - nr).ToString(); } We can understand that you are decrementing a number and displaying it in a textBox. Your question is "string (nr) has change to 2 and next time 3". - nr is not a string, nr is a static int So what do you mean by this? Do you mean the text is being incremented instead of decrementing. Well this is impossible as you see that there is no addition code in myTimer_Tick(). Best Regards.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups