Question Code isn't working proprly

Jivkov66

New member
Joined
Jan 24, 2021
Messages
2
Programming Experience
Beginner
Hello! Here's the following code that I have problem with. The problem is that everytime I put in Timer.start(); and Timer.stop(); the code doesn't work properly. When I hold and release shift the text is stuck at "Shift is held" and the Timer isn't starting at all. Is there any way to fix that issue?
C#:
if (CheckBox.Checked == true)
            {
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    label1.Text = "Shift is held";
                    Timer.Start();
                }
                else
                {
                    label1.Text = "Shift is released";
                    Timer.Stop();
                }
            }
 
Solution
As for me, implementing a version of your code in my own form works correctly. Whenever the checkbox is changed to checked and the Shift key is pressed, then the timer is stopped. When the checkbox is changed to checked and the Shift key is not pressed, then the timer is started. I can tell that the timer is running when the current time is updated every half second.

C#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleWinForms
{
    class TestForm : Form
    {
        Timer _timer = new Timer();

        public TestForm()
        {
            var flow = new FlowLayoutPanel()
            {
                FlowDirection = FlowDirection.TopDown,
                Dock =...
How do you know the timer is not started?
 
How do you know the timer is not started?
Sorry, I made a little mistake by swapping the position of the start/stop timer. This is the right code. Regarding to your question how I know if the timer isn't started, I am using the timer so my program can automatically click for me. While using the following code the text is saying "Shift is held" even when I release the shift key and I assume that is the reason why the timer isn't starting. I am asking if someone could help me resolve the issue or if possible to give me an alternative code to use.

C#:
if (CheckBox.Checked == true)
            {
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    label1.Text = "Shift is held";
                    Timer.Stop();
                }
                else
                {
                    label1.Text = "Shift is released";
                    Timer.Start();
                }
            }
 
We don't know what the context of that code you presented above is in. We don't know when will the next time that code above would be called say when your labels would be updated. How do we know if your code above is being called at the right times?

Set breakpoints on line 6 and line 11. If those breakpoints are not hit, then yes, the timers are not being stopped or started. Otherwise, I highly doubt that there would be a bug at this late date that would cause the timers in WinForms to suddenly not work correctly.

Again, I ask you how do you know that the timers are not working. Did you set a breakpoint on your tick event and your event is not being called?
 
As for me, implementing a version of your code in my own form works correctly. Whenever the checkbox is changed to checked and the Shift key is pressed, then the timer is stopped. When the checkbox is changed to checked and the Shift key is not pressed, then the timer is started. I can tell that the timer is running when the current time is updated every half second.

C#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleWinForms
{
    class TestForm : Form
    {
        Timer _timer = new Timer();

        public TestForm()
        {
            var flow = new FlowLayoutPanel()
            {
                FlowDirection = FlowDirection.TopDown,
                Dock = DockStyle.Fill,
                AutoSize = true,
            };

            SuspendLayout();
            flow.SuspendLayout();

            var lblShiftState = new Label() { AutoSize = true };
            var lblTime = new Label() { AutoSize = true };

            _timer.Interval = 500;
            _timer.Tick += (o, e) => lblTime.Text = DateTime.Now.ToString();

            var chkWatchShift = new CheckBox() { Text = "Check Shift Key" };
            chkWatchShift.CheckedChanged += (o, e) => WatchShift(chkWatchShift, lblShiftState);


            flow.Controls.Add(chkWatchShift);
            flow.Controls.Add(lblShiftState);
            flow.Controls.Add(lblTime);

            Controls.Add(flow);

            flow.ResumeLayout(false);
            ResumeLayout(false);
        }

        void WatchShift(CheckBox chk, Label lbl)
        {
            if (chk.Checked)
            {
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    lbl.Text = "Shift key is down";
                    _timer.Stop();
                }
                else
                {
                    lbl.Text = "Shift key is up";
                    _timer.Start();
                }
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new TestForm());
        }
    }
}
 
Solution

Latest posts

Back
Top Bottom