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. A timer problem?

A timer problem?

Scheduled Pinned Locked Moved C#
graphicsdockerhelpquestion
5 Posts 2 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.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    Hi, I have a listbox with three values on it and a button on my form. When I pressed the button the listbox have to select the next item after 2 seconds. Then select the next one after 2 seconds... And so on... Here is my unworking below code: Anyhelp would be greatly appreciated... Thank you indeed... Cem Louis using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Timers; using System.Threading; namespace WindowsApplication14 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Timers.Timer timerClock = new System.Timers.Timer(); /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } public void InitializeTimer() { this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer); this.timerClock.Interval = 2000; this.timerClock.Enabled = true; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(8, 112); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "Run"; this.button1.Click += new System.EventHandler(this.butto

    S 1 Reply Last reply
    0
    • C cemlouis

      Hi, I have a listbox with three values on it and a button on my form. When I pressed the button the listbox have to select the next item after 2 seconds. Then select the next one after 2 seconds... And so on... Here is my unworking below code: Anyhelp would be greatly appreciated... Thank you indeed... Cem Louis using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Timers; using System.Threading; namespace WindowsApplication14 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Timers.Timer timerClock = new System.Timers.Timer(); /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } public void InitializeTimer() { this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer); this.timerClock.Interval = 2000; this.timerClock.Enabled = true; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(8, 112); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "Run"; this.button1.Click += new System.EventHandler(this.butto

      S Offline
      S Offline
      smithriver
      wrote on last edited by
      #2

      I am no guru, but it looks like the problem may be in nextvalue(). Perhaps it was a cut & paste error, but the "for" statement is incomplete. The other problem could be the statement "listBox1.Items.Count-i"; if i is 0, the command would error because the count is always 1 larger than the indexing. How I would attempt to rotate through the items would be: int i = listBox1.SelectedIndex + 1; if ( i == listBox1.Items.Count ) i = 0; listBox1.SetSelected( i, true ); Hope this helps!

      C 1 Reply Last reply
      0
      • S smithriver

        I am no guru, but it looks like the problem may be in nextvalue(). Perhaps it was a cut & paste error, but the "for" statement is incomplete. The other problem could be the statement "listBox1.Items.Count-i"; if i is 0, the command would error because the count is always 1 larger than the indexing. How I would attempt to rotate through the items would be: int i = listBox1.SelectedIndex + 1; if ( i == listBox1.Items.Count ) i = 0; listBox1.SetSelected( i, true ); Hope this helps!

        C Offline
        C Offline
        cemlouis
        wrote on last edited by
        #3

        Hi Smith, Not working :(

        S 2 Replies Last reply
        0
        • C cemlouis

          Hi Smith, Not working :(

          S Offline
          S Offline
          smithriver
          wrote on last edited by
          #4

          Two things: 1) Make sure the nextvalue() has the code I included earlier: private void nextvalue() { int i = listBox1.SelectedIndex + 1; if ( i == listBox1.Items.Count ) i = 0; listBox1.SetSelected( i, true ); } 2) The timer event that is being used could be incorrect. When I add a timer to my test project, then click on the Events button (the "lightning bolt") in the Properties grid, the only event displayed is the Tick event. Double-click on the Tick title and it will auto-magically add the event to the code. Put the "nextvalue()" call in that subroutine, then try running it. Hope this helps!

          1 Reply Last reply
          0
          • C cemlouis

            Hi Smith, Not working :(

            S Offline
            S Offline
            smithriver
            wrote on last edited by
            #5

            Have you been able to get the timer working? I apologize for overlooking the possibility that you were probably trying to learn about handlers, more so than getting the timer to work. As fate would have it, I have had to work with timers and handlers in the past week. So, I took another look at the original code posted. I made three changes and was able to get the program to work: 1) Modify nextvalue() private void nextValue() { int i = listBox1.SelectedIndex + 1; if ( i == listBox1.Items.Count ) i = 0; listBox1.SetSelected( i, true ); } 2) Add a call to InitializeTimer() in the form constructor: This is a public method, so another program could have been calling it. But since I cannot tell, I added a call in the form constructor: // TODO: Add any constructor code after InitializeComponent call InitializeTimer(); 3) Modify the handler definition and code: The definition of the handler is still in InitializeTimer(). I was only able to find a handler for "Tick", not "Elapsed". this.timerClock.Tick += new EventHandler(timerClock_Tick); The neat thing about adding the handler is that Visual Studio will do most of the work for you. After typing "+=" you will be prompted to "Press TAB to insert" the remaining of the command. You will immediately be prompted a second time to "Press TAB to generate handler". After generating the handler, add the call to nextvalue() within that event handler. private void timerClock_Tick(object sender, EventArgs e) { nextValue(); } Hope this helps!

            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