473,503 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A timer problem?

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>
/// Summary description for Form1.
/// </summary>
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();
/// <summary>
/// Required designer variable.
/// </summary>
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;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(136, 142);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add("Arthur");
listBox1.Items.Add("Alex");
listBox1.Items.Add("Amie");
listBox1.Update();
}

private void OnTimer(object source, ElapsedEventArgs e)
{
this.nextvalue();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.nextvalue();
}

private void nextvalue()
{
for(int i = 0;i<listBox1.Items.Count;i++)
{
listBox1.SetSelected(listBox1.Items.Count-i,true);
}
Nov 16 '05 #1
3 1362
Hi Cem
You didnt start the timer.

In your button1_click code you need to add the line
timerClock.Start();

That should work
Mark

"Cem Louis" <ce******@phreaker.net> wrote in message
news:12**************************@posting.google.c om...
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>
/// Summary description for Form1.
/// </summary>
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();
/// <summary>
/// Required designer variable.
/// </summary>
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;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(136, 142);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add("Arthur");
listBox1.Items.Add("Alex");
listBox1.Items.Add("Amie");
listBox1.Update();
}

private void OnTimer(object source, ElapsedEventArgs e)
{
this.nextvalue();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.nextvalue();
}

private void nextvalue()
{
for(int i = 0;i<listBox1.Items.Count;i++)
{
listBox1.SetSelected(listBox1.Items.Count-i,true);
}

Nov 16 '05 #2
Hi Mark,

Thanks for the suggestion. I have made some changes code becomes like
below but It is not working properly... I mean not working what I
want... (It(the selector line) must go to next listbox item in
determined second after clicking the run button...)???

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>
/// Summary description for Form1.
/// </summary>
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();
/// <summary>
/// Required designer variable.
/// </summary>
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 = 5000;
this.timerClock.Enabled = true;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(136, 142);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add("Arthur");
listBox1.Items.Add("Alex");
listBox1.Items.Add("Amie");
listBox1.Update();
}

private void OnTimer(object source, ElapsedEventArgs e)
{
this.nextvalue();
}

private void button1_Click(object sender, System.EventArgs e)
{
timerClock.Start();
this.nextvalue();
}

private void nextvalue()
{
for(int i = 0;i<listBox1.Items.Count;i++)
{
listBox1.SetSelected(i,true);
}
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
I think i understand you to mean that each time the timer fires you want it
to move one down the list?

Currently your next value is set to go through every item in the list each
time the nextvalue() is called.

next value should just increment the item index in the list box by one.
something like
listbox1.setselected(listbox1.selectedindex++, true);

hope it helps
Mark

"Cem Louis" <ce******@phreaker.net> wrote in message
news:ub**************@TK2MSFTNGP14.phx.gbl...
Hi Mark,

Thanks for the suggestion. I have made some changes code becomes like
below but It is not working properly... I mean not working what I
want... (It(the selector line) must go to next listbox item in
determined second after clicking the run button...)???

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>
/// Summary description for Form1.
/// </summary>
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();
/// <summary>
/// Required designer variable.
/// </summary>
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 = 5000;
this.timerClock.Enabled = true;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(136, 142);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add("Arthur");
listBox1.Items.Add("Alex");
listBox1.Items.Add("Amie");
listBox1.Update();
}

private void OnTimer(object source, ElapsedEventArgs e)
{
this.nextvalue();
}

private void button1_Click(object sender, System.EventArgs e)
{
timerClock.Start();
this.nextvalue();
}

private void nextvalue()
{
for(int i = 0;i<listBox1.Items.Count;i++)
{
listBox1.SetSelected(i,true);
}
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7430
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
11
2544
by: Steve Jorgensen | last post by:
I've recently been playing with some UI ideas that require the user of a timer to drive animation. The problem I'm having is that Access routinely stops firing timer events for long periods of...
9
7246
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
4
11087
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
4
1674
by: Daniel | last post by:
Hey guys Here is what i want to do. I have made a multiplayer game that needs to when more than one player is ready start a countdown that the clients can see, so players can still join in this...
4
4617
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
8
6833
by: KnighT | last post by:
I have a .net service that runs a System.Threading.Timer. The delegate points to the function that the service should execute when the timer elapses. Problem: The timer is not ticking. I have...
8
2636
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
16
3227
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
2
4518
by: Johnny Jörgensen | last post by:
I've got a process I want to run in a thread separate from my main application thread, so I've used a backgroundworker component, and in frmMain.Load I invoke the code using...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.