Search code examples
c#formstimer

C# Get Button Click from another Form


I want to make that you can start a Timer from Form2 and in Form1 the Timer is counting down. Thats what I already made. Now I want just that you can Stop that Timer in Form1

Form1:

        public static Form1 instance;
        public Label tb1;
        // public Timer tm1;
        public Form1()
        {
            InitializeComponent();
            instance = this;
            tb1 = labelCountdown;
        }

        private void buttonStopSTD_Click(object sender, EventArgs e) // Stops the Shutdown
        {
            Process.Start("shutdown", "-a");
        }

Form2:

        private void button10m_Click(object sender, EventArgs e)
        {
            seconds = Convert.ToInt32(600);
            timer1.Start();
            Process.Start("shutdown", "/s /t 600");
        }

        private void timer1_Tick(object sender, EventArgs e) // Timer
        {
            string off = "Shutdown not set";
            if (seconds < 1) // If Timer hits 0 it turns the Timer off and prints "Shutdown not set"
            {
                timer1.Stop();
                labelCdown.Text = off;
                Form1.instance.tb1.Text = ("Shutdown not set");
            }
            else // If the Timer is above 0 it normally works
            {
                TimeSpan time = TimeSpan.FromSeconds(seconds--);
                string str = time.ToString(@"hh\:mm\:ss");
                labelCdown.Text = str;
                Form1.instance.tb1.Text = ("Shutdown in: " + str);
            }
        }

I tried simply stoping the Timer over Form1: Form1.instance.Timer.Stop();


Solution

  • It seems like you want to start a countdown timer in Form2 and be able to stop it from Form1. However, your current code doesn't seem to have a Timer object exposed in Form1, and Form1.instance.Timer.Stop() won't work because you haven't defined a Timer property. To achieve your goal, you can create a Timer object in Form2 and expose methods to start and stop it from both Form1 and Form2.

    Here's how you can modify your code to achieve this:

    Form 1:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void buttonStopSTD_Click(object sender, EventArgs e)
        {
            // Stop the timer in Form2
            Form2.instance.StopTimer();
        }
    }
    

    Form 2:

        public partial class Form2 : Form
    {
        private Timer timer1 = new Timer();
        public static Form2 instance;
    
        public Form2()
        {
            InitializeComponent();
            instance = this;
            timer1.Interval = 1000; // 1 second
            timer1.Tick += timer1_Tick;
        }
    
        public void StartTimer(int seconds)
        {
            // Start the timer with the specified number of seconds
            timer1.Start();
            // Your other timer setup logic here
        }
    
        public void StopTimer()
        {
            timer1.Stop();
            // Your logic to handle stopping the timer here
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            // Your timer tick logic
        }
    
        private void button10m_Click(object sender, EventArgs e)
        {
            int seconds = Convert.ToInt32(600);
            StartTimer(seconds);
        }
    }
    

    In Form2, I've added methods StartTimer and StopTimer that allow you to start and stop the timer. You can call these methods from Form1 to control the timer in Form2. Make sure to adapt your timer logic inside these methods accordingly.

    This approach allows you to start and stop the timer in Form2 from Form1 as needed.