C# 从另一个表单获取按钮点击

问题描述 投票:0回答:1

我想让你可以从 Form2 启动一个计时器,而在 Form1 中计时器正在倒计时。 这就是我已经做的。 现在我只想你可以在 Form1 中停止该计时器

表格1:

        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");
        }

表格2:

        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);
            }
        }

我尝试简单地停止 Form1 上的计时器:Form1.instance.Timer.Stop();

c# forms timer
1个回答
0
投票

您似乎想在 Form2 中启动倒计时器并能够从 Form1 中停止它。但是,您当前的代码似乎没有在 Form1 中公开 Timer 对象,并且 Form1.instance.Timer.Stop() 将不起作用,因为您尚未定义 Timer 属性。为了实现您的目标,您可以在 Form2 中创建一个 Timer 对象,并公开方法以从 Form1 和 Form2 中启动和停止它。

以下是如何修改代码来实现此目的:

表格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();
    }
}

表格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);
    }
}

在 Form2 中,我添加了方法

StartTimer
StopTimer
,允许您启动和停止计时器。您可以从 Form1 调用这些方法来控制 Form2 中的计时器。确保相应地调整这些方法中的计时器逻辑。

此方法允许您根据需要启动和停止 Form1 中 Form2 中的计时器。

© www.soinside.com 2019 - 2024. All rights reserved.