-1

I having problem on the C# Windows Forms. I have created a timer and a button to act as a timer lamp. Now I have created the timer and the timer value can get successfully. But I dont know why the button backcolor cannot change as the setted on the code.

Below is my whole codes.

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;


namespace TimerLamp
{
    public partial class Form1 : Form
    {
        System.Timers.Timer t;
        int s, c, x;
        String sec;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            t = new System.Timers.Timer();
            t.Interval = 1000; //1s
            t.Elapsed += OnTimeEvent;
            //t.Enabled = true;
        }

        private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            s += 1;
            sec = s.ToString();
            if (s == 23)
            {
                s = 0;
                c += 1;
            }
            TimerTextBox.Invoke((MethodInvoker)(() => TimerTextBox.Text = s.ToString()));
            TimerTextBox.Invoke((MethodInvoker)(() => CounterTextBox.Text = c.ToString()));
            sec = s.ToString();
        }

        private void TurnOnLamp_CheckedChanged(object sender, EventArgs e)
        {
           
            if (TurnOnLamp.Checked == true)
            {
                x = Convert.ToInt16(sec);
                if (x >= 0 && x <7)
                {
                    btnLamp.BackColor = Color.Green;
                }
                if (x > 8 && x < 19)
                {
                    btnLamp.BackColor = Color.Red;
                }
                else if (x >= 19)
                {
                    btnLamp.BackColor = Color.Green;
                }
                else
                {
                    btnLamp.BackColor = Color.Red;
                }
            }
            if (TurnOnLamp.Checked == false)
            {
                btnLamp.BackColor = Color.Red;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "Start")
            {
                t.Start();
                if (c >= 30)
                {
                    MessageBox.Show("Limit reached");
                    return;
                }
                btnStart.Text = "Stop";

            }
            else if (btnStart.Text == "Stop")
            {
                t.Stop();
                btnStart.Text = "Start";
            }
        }

        private void btnLamp_Click(object sender, EventArgs e)
        {

        }

        private void TimerTextBox_TextChanged(object sender, EventArgs e)
        {

        }


        private void CounterTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Stop();
            Application.DoEvents();
        }

        
  

    }

       
}

Can someone help me to solve this problem?

  • Where does `sec` come from? Did you debug and check that all variables have expected values? Can you add the timer-event handler's code? – Fildor Aug 30 '21 at 08:14
  • There is no timer in the code you posted. What is `sec` and where is it set? (btw: to compare a `bool` to `true` or `false` is useless). Have you tried to [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) your code? – René Vogt Aug 30 '21 at 08:16
  • The program can run, just the button colour cannot change. So how should I debug it? Sorry as I new in programming. – EXCITED AID Aug 30 '21 at 08:19
  • 3
    Why not use the [System.Windows.Forms.Timer](https://learn.microsoft.com/dotnet/api/system.windows.forms.timer) ? –  Aug 30 '21 at 08:23
  • 3
    You are using a subotimal timer implemenatation. When dealing with windows forms, it's beneficial (meh, let's say "more convenient") to use a windows forms timer. – Fildor Aug 30 '21 at 08:23
  • Does this answer your question? [How to access a WinForms control from another thread i.e. synchronize with the GUI thread?](https://stackoverflow.com/questions/58657831/how-to-access-a-winforms-control-from-another-thread-i-e-synchronize-with-the-g) and [Synchronize Timer with real time](https://stackoverflow.com/questions/62848329/synchronize-timer-with-real-time) –  Aug 30 '21 at 08:24
  • There are gaps in the code anyway. At second values 7 and 8 the code won't work. And if you need to check a boolean for true and then false, don't add the `== true` part and just use `else` instead of the second `if` that checks the false condition. There's also some `else` keywords missing in that evaluation of the seconds, meaning it'll do unnecessary extra checks. – Nyerguds Aug 30 '21 at 08:25
  • I just follow the tutorial from youtube. Now I dont know how to change the button timer as the color can only changed when the checkbox is true. So can I still continue to have the answer for it or i need to use the winforms timer? – EXCITED AID Aug 30 '21 at 08:30

1 Answers1

0

When you press the button once, it starts counting in the btnLamp_Click class. And when the checkbox is checked, the button blinks as following code.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TimerLamp
{
    public partial class Form1 : Form
    {
        Timer t;
        int s, c, x;
        String sec;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            t = new Timer();
            t.Interval = 1000; //1s
            t.Tick += OnTimeEvent;
            //t.Enabled = true;
        }

        private void OnTimeEvent(object sender, EventArgs e)
        {
            s += 1;
            sec = s.ToString();
            if (s == 9)
            {
                s = 0;
                c += 1;
            }
            textBox1.Text = "sec = " + sec;
            textBox2.Text = "c = " + c;

            if (TurnOnLamp.Checked == true)
            {
                x = s;
                if (x == 0) btnLamp.BackColor = Color.Green;
                if (x == 2) btnLamp.BackColor = Color.Red;
                if (x == 4) btnLamp.BackColor = Color.Blue;
                if( x == 6) btnLamp.BackColor = Color.LightCyan;
            }
            else
            {
                btnLamp.BackColor = Color.Gray;
            }
        }

        private void btnLamp_Click(object sender, EventArgs e)
        {
            if (btnLamp.Text == "Start")
            {
                t.Start();
                if (c >= 2)
                {
                    MessageBox.Show("Limit reached");
                    t.Stop(); 
                }
                btnLamp.Text = "Stop";
            }
            else if (btnLamp.Text == "Stop")
            {
                t.Stop();
                btnLamp.Text = "Start";
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Stop();
            Application.DoEvents();
        }
    }
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75