I want a textbox on the form to show the current time. The code I have written follows. But it does not work and I don't know why :( Can someone please point out the error. - 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;
-
using System.Timers;
-
using System.Threading;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
private System.Timers.Timer myTimer = new System.Timers.Timer();
-
private TextBox textBox2;
-
-
private System.ComponentModel.IContainer components = null;
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
protected override void Dispose(bool disposing)
-
{
-
if (disposing && (components != null))
-
{
-
components.Dispose();
-
}
-
base.Dispose(disposing);
-
}
-
-
private void InitializeComponent()
-
{
-
this.textBox2 = new System.Windows.Forms.TextBox();
-
this.SuspendLayout();
-
//
-
// textBox2
-
//
-
this.textBox2.Location = new System.Drawing.Point(84, 186);
-
this.textBox2.Name = "textBox2";
-
this.textBox2.Size = new System.Drawing.Size(100, 20);
-
this.textBox2.TabIndex = 0;
-
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
-
//
-
// Form1
-
//
-
this.ClientSize = new System.Drawing.Size(284, 264);
-
this.Controls.Add(this.textBox2);
-
this.Name = "Form1";
-
this.ResumeLayout(false);
-
this.PerformLayout();
-
this.Load += new System.EventHandler(this.Form1_Load);
-
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
-
myTimer.Interval = 1000;
-
myTimer.Start();
-
}
-
-
public void DisplayTimeEvent( object source, ElapsedEventArgs e )
-
{
-
textBox2.Text = DateTime.Now.ToString();
-
}
-
-
private void textBox2_TextChanged(object sender, EventArgs e)
-
{
-
-
}
-
}
-
}
- 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;
-
using System.Timers;
-
using System.Threading;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
private TextBox textBox2;
-
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
-
-
private System.ComponentModel.IContainer components = null;
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
protected override void Dispose(bool disposing)
-
{
-
if (disposing && (components != null))
-
{
-
components.Dispose();
-
}
-
base.Dispose(disposing);
-
}
-
-
private void InitializeComponent()
-
{
-
this.textBox2 = new System.Windows.Forms.TextBox();
-
this.SuspendLayout();
-
//
-
// textBox2
-
//
-
this.textBox2.Location = new System.Drawing.Point(84, 186);
-
this.textBox2.Name = "textBox2";
-
this.textBox2.Size = new System.Drawing.Size(100, 20);
-
this.textBox2.TabIndex = 0;
-
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
-
//
-
// Form1
-
//
-
this.ClientSize = new System.Drawing.Size(284, 264);
-
this.Controls.Add(this.textBox2);
-
this.Name = "Form1";
-
this.ResumeLayout(false);
-
this.PerformLayout();
-
this.Load += new System.EventHandler(this.Form1_Load);
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
ElapsedEventHandler(DisplayTimeEvent);
-
myTimer.Interval = 1000;
-
myTimer.Start();
-
myTimer.Tick += new EventHandler(DisplayTimeEvent);
-
}
-
-
public void DisplayTimeEvent( object sender, EventArgs e )
-
{
-
this.textBox2.Text = DateTime.Now.ToString();
-
}
-
-
private void textBox2_TextChanged(object sender, EventArgs e)
-
{
-
-
}
-
}
-
}
8 2515 TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Put a breakpoint on line 67 then run/debug/F5 the project.
If the execution breaks on the line, then you know your timer is running and the event is being called.
If execution doesn't ever break on this line, then you point to start searching backward from:
IE: The handler is never called, therefore the Timer.Elapsed event never raises
Personally, for something as course as 1,000 miliseconds I'd just use a Windows.Forms.Timer and the .Tick event.
I'll keep the [code] tags in mind from next time. I put a breakpoint on line 67, but the execution did not break. So I guess that the handler is not getting called. Do you know why could that be happening?
What I really want to create is a countdown timer for an elapsed time. So I trying the get the basic up-count timer right before I do that.
I managed to execute a call to a handler...but now I am getting another error:
$exception {"Cross-thread operation not valid: Control 'textBox2' accessed from a thread other than the thread it was created on."} System.Exception {System.InvalidOperationException}
Well there ya go... The timer and the form have not been created on the same thread.
Let me again suggest using a System.Windows.Forms.Timer and making that timer a control on Form1
For a countdown timer you can create a new DateTime object set to a time in the future then subtract DateTime.Now from that future object. The difference is the remaining time which you will display as you countdown.
Yo....I got it to work. Thanks :)
Congratulations! Since this is a place of learning and sharing, would you mind sharing your working solution with the rest of the forum in case someone else (now or in the future) has a similar problem?
- 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;
-
using System.Timers;
-
using System.Threading;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
private TextBox textBox2;
-
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
-
-
private System.ComponentModel.IContainer components = null;
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
protected override void Dispose(bool disposing)
-
{
-
if (disposing && (components != null))
-
{
-
components.Dispose();
-
}
-
base.Dispose(disposing);
-
}
-
-
private void InitializeComponent()
-
{
-
this.textBox2 = new System.Windows.Forms.TextBox();
-
this.SuspendLayout();
-
//
-
// textBox2
-
//
-
this.textBox2.Location = new System.Drawing.Point(84, 186);
-
this.textBox2.Name = "textBox2";
-
this.textBox2.Size = new System.Drawing.Size(100, 20);
-
this.textBox2.TabIndex = 0;
-
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
-
//
-
// Form1
-
//
-
this.ClientSize = new System.Drawing.Size(284, 264);
-
this.Controls.Add(this.textBox2);
-
this.Name = "Form1";
-
this.ResumeLayout(false);
-
this.PerformLayout();
-
this.Load += new System.EventHandler(this.Form1_Load);
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
ElapsedEventHandler(DisplayTimeEvent);
-
myTimer.Interval = 1000;
-
myTimer.Start();
-
myTimer.Tick += new EventHandler(DisplayTimeEvent);
-
}
-
-
public void DisplayTimeEvent( object sender, EventArgs e )
-
{
-
this.textBox2.Text = DateTime.Now.ToString();
-
}
-
-
private void textBox2_TextChanged(object sender, EventArgs e)
-
{
-
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Dan |
last post by:
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a...
|
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;...
|
by: Grahmmer |
last post by:
I have a few timers that are added to a form at runtime. I can handle the
event fine, but I cannot identify which timer fired. Is there a way to do
this?
Timer Creation:
-------------...
|
by: John David Thornton |
last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and
I've coded it as shown below. However, when I install the service and then
start it in MMC, I get a peculiar message:
...
|
by: Gina_Marano |
last post by:
I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different...
|
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...
|
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...
|
by: Hotrod2000 |
last post by:
I'm quite new to programming but I'm having problems getting a timer
to work in visual studio.net
I've created a timer on a form, enabled it and then typed the
following code (from the mdsn...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |