Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a form from a static method?

Status
Not open for further replies.

mattdrinks

Technical User
Oct 2, 2002
43
GB
Is this possible...

I have an event that is fired by a timer, but I would like that event to update a form as it goes so the user can see what is happening.

I have the following code:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;

namespace SendingAgent
{
    public partial class Form1 : Form
    {
        public static System.Timers.Timer aTimer = new System.Timers.Timer();

        public Form1()
        {
            InitializeComponent();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 2.5 seconds (2500 milliseconds).
            aTimer.Interval = 2500;
            aTimer.Enabled = false;
        }
        
        // Specify what you want to happen when the Elapsed event is 
        // raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            MessageBox.Show("Hello World!");
        }

        
        private void btnStart_Click(object sender, EventArgs e)
        {
            Form1.aTimer.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            Form1.aTimer.Enabled = false;
        }
    }
}

But I can not access the form from inside the OnTimedEvent Method becuase it is static.

eg.
Code:
Form1.txtStatus.Text = "Started..."
will not work.

does anyone have any ideas how I can achieve this.

Thanks for your help,
Matt

P.S. I am using Dot Net 2.0 (Beta 2)
 
If you want to manipulate items on the form, you can't do it in a static method. Does your Timer have to be static?

--Brad

"Life is too important to be taken seriously" --Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top