Doug Hauf
Doug Hauf

Reputation: 3213

Placing a timer on the form to update a Label1.Text field?

I have a simple form with a Label1 on the form. I want a timer that will every 10 seconds update the label and change the value to increase by one or to whatever text I want to place in the text box.

This is the code that I am using. I have been able to have it flash up a message box and then to update the value in the text of the message box but the form will not appear unless I end the timer. I want the form to display and update a label1.Text fields value when the timer is called.

Ignore the graphics portion of the code.

Program Code:

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.Threading;

namespace DDHBindingExcelSheet
{
    public partial class Form4 : Form
    {
        static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        static int alarmCounter = 1;
        static bool exitFlag = false;

        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            time();
        }

        private void TimerEventProcessor(Object myObject,
                                                EventArgs myEventArgs)
        {
            myTimer.Stop();

            // Displays a message box asking whether to continue running the timer. 
           // if (MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
           //    MessageBoxButtons.YesNo) == DialogResult.Yes)
           // {
                // Restarts the timer and increments the counter.
                alarmCounter += 1;
                l.Text = alarmCounter.ToString(); ;
                myTimer.Enabled = true;
         //   }
         //   else
         //   {
                // Stops the timer.
          //      exitFlag = true;
         //   }
        }

        private void time()
        {

            /*Adds the event and the event handler for the method that will 
            process the timer event to the timer.*/
            myTimer.Tick += new EventHandler(TimerEventProcessor);

            // Sets the timer interval to 5 seconds.
            myTimer.Interval = 5000;
            myTimer.Start();

            // Runs the timer, and raises the event. 
            while (exitFlag == false)
            {
                // Processes all the events in the queue.
                Application.DoEvents();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g;
            Pen myPen = new Pen(Color.Black, 2);
            Point sp = new Point(73, 73);//starting point sp
            Point ep = new Point(250, 240);//ending point ep 
            g = this.CreateGraphics();//tells compiler that we are going to draw on this very form
            g.DrawLine(myPen, sp, ep);

            myPen.Width = 1;
            myPen.Color = Color.Red;
            g.DrawRectangle(myPen, 240, 230, 25, 25);
            myPen.Width = 1;
            myPen.Color = Color.Black;
            g.DrawLine(myPen, sp, ep);
            g.DrawEllipse(myPen, 0, 0, 500, 500);//you can see here we use 30,30 same width and height to draw a circle if they were different an ellipse would be drawn where as x and y are the upper right coordinates of a rectangle bounding this circle. 
            g.DrawEllipse(myPen, 250, 240, 5, 5);
        }
    }
}

Upvotes: 0

Views: 351

Answers (1)

Johnbot
Johnbot

Reputation: 2179

Simply do not EVER call Application.DoEvents()

Ïf you remove the following the program will work as intended:

// Runs the timer, and raises the event. 
while (exitFlag == false)
{
    // Processes all the events in the queue.
    Application.DoEvents();
}

Upvotes: 2

Related Questions