完成计数时间后重新打开表格

时间:2016-03-11 07:16:18

标签: c# winforms timer

我在这里遇到一些代码问题,就像你有一个最大化和控制屏幕的表格,当你登录时你有时间使用电脑,在时间停止或结束时计算表格的时间将会结束再次最大化。

问题是,如果计时器停止或计数

,我不知道如何捕捉

谢谢你的时间!

private void Monitoring_Load(object sender, EventArgs e)
{
    try
    {            
        fs.Maximize(this, this, e);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), "Errors");
    }
}

private void btn_login_Click(object sender, EventArgs e)
{

            fs.Restore(this);
            T.Show();
            if (T.IsDisposed == true)
            {
                fs.Maximize(this, this, e);
            }
            else
            { fs.Restore(this); }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Errors");
        }
}


/////////////////////////////////////



public void Maximize(Form targetForm, object sender, EventArgs e)
{

    //////////////////////////////////
    ct.hidetaskbar();
    cu.DisableTaskmanager();
    cu.DisableUserSwitching();
    cu.DisableWorkstationLock();
    ck.KeyboardHook(this, e);
    ////////////////////////////////////
    targetForm.WindowState = FormWindowState.Maximized;
    targetForm.FormBorderStyle = FormBorderStyle.None;
    targetForm.TopMost = true;
    targetForm.ShowInTaskbar = false;
    targetForm.ControlBox = false;
    FullScreen.SetWinFullScreen(targetForm.Handle);
}
////////////////////////////////////////

 public void Restore(Form targetForm)
{
    ////////////////////////////////////
    cu.EnableTaskManager();
    cu.EnableUserSwitching();
    cu.EnableWorkstationLock();
    ct.showtaskbar();
    ck.ReleaseKeyboardHook();
    ///////////////////////////////////
    targetForm.ControlBox = false;
    targetForm.FormBorderStyle = FormBorderStyle.FixedSingle;
    targetForm.WindowState = FormWindowState.Minimized;
    targetForm.ShowInTaskbar = true;
    targetForm.Visible = true;
    targetForm.Hide();
}

///////////////////////////////////



public partial class Timer : Form
{
    int h;
    int m = 15;
    int s;

    public Timer()
    {
        InitializeComponent();
        Rectangle workingArea = Screen.GetWorkingArea(this);
        this.Location = new Point(workingArea.Right - Size.Width,
                                  workingArea.Bottom - Size.Height);
        timer1.Enabled = true;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (h > 0)
        {

            if (m > 0)
            {
                if (s > 0)
                {
                    s--;
                }
                else
                {
                    s = 59;
                    m--;
                }
            }
            else
            {
                m = 59;
                h--;
            }


        }
        else
            if (h == 0)
        {
            if (m > 0)
            {
                if (s > 0)
                {
                    s--;
                }
                else
                {
                    s = 59;
                    m--;
                }
            }
            else
            {
                s--;
            }
            this.lb_h.Text = h.ToString();
            this.lb_m.Text = m.ToString();
            this.lb_s.Text = s.ToString();
        }
        if (h == 0 && m == 0 && s == 0)
        {
            timer1.Stop();
            this.Close();
        }
    }

    private void btn_end_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        this.Close();
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用Timer组件并在表单中设置结束时间,并在Tick事件检查中DateTime.Now是否大于或等于结束时间,然后是给定的时间结束了。

在下面的示例中,我假设您有一个PasswordForm,当密码正确时会返回DialogResult.OK。我会在开始时显示密码表单,并在时间结束时显示:

DateTime endTime;

private void MainForm_Load(object sender, EventArgs e)
{
    ShowPasswordForm();
}

private void ShowPasswordForm()
{
    timer1.Stop();
    var f = new PasswordForm();
    var passed = false;
    while (!passed)
    {
        if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            passed = true;
    }
    var startTime = DateTime.Now;
    endTime = startTime.AddMinutes(1); //I used 1 minute for test, add the time you need.
    timer1.Interval = 1000;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    var diff = endTime.Subtract(DateTime.Now);
    this.Text = string.Format("Remaining: {0} d {1:D2}:{2:D2}:{3:D2}",
                            diff.Days, diff.Hours, diff.Minutes, diff.Seconds);

    if (DateTime.Now >= endTime)
    {
        timer1.Stop();
        this.Text = "Remaining: 0 d 00:00:00";
        MessageBox.Show("Time is over.");
        ShowPasswordForm();
    }
}

答案 1 :(得分:0)

如果您在表单中需要它,可以使用工具栏中的控件,如下所示:

class Timer : Form {
    var myTimer = new System.Windows.Forms.Timer();

    private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
        // If you want handle interval once call Stop method, if not remove this line
        myTimer.Stop();

        // Call waht you want
    }

    public void Run() {
        myTimer.Tick += TimerEventProcessor;

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