c# - 作业2:计时器

总体视图 & 废话集结区

1

我记得这是c#最上头的时候,课上敲敲敲还不给劲给,下课了之后还自己鼓捣,对的错的吧所有可能的(至少当时是有可能的东西都搞了一遍/反正是完美解决的,我觉得真的尚可

当时是用来三种方法来着,热情似火呀

hah

代码(全)

Form1.cs

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 MyTimer
{
    public partial class Form1 : Form
    {
        private int _nowSecond = 0;


        public Form1()
        {
            InitializeComponent();
            _nowSecond = 0;

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = true;

            if(textBoxCount.Text.Trim()==String.Empty)
            {
                _nowSecond = 0;
                
                labelHour.Text = "00";
                labelMinute.Text = "00";
                labelSecond.Text = "00.0";
                
            }

            else
            {
                _nowSecond = Convert.ToInt32(textBoxCount.Text)*10;
                int hour = _nowSecond / 36000;
                int minute = (_nowSecond % 36000) / 600;
                double second = (_nowSecond % 36000) % 600 / 10.0;
                
                labelHour.Text = hour.ToString("00");
                labelMinute.Text = minute.ToString("00");
                labelSecond.Text = second.ToString("00.0");

            }
            labelColon1.Visible = true;
            labelColon2.Visible = true;
            buttonStart.Enabled = false;
            buttonPauseContinue.Enabled = true;
            buttonStop.Enabled = true;
            buttonPauseContinue.Text = "暂停";

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (textBoxCount.Text.Trim() == String.Empty)
            {
                _nowSecond++;
            }
            else 
            {
                _nowSecond--;
                if (_nowSecond <= 0) 
                {
                    timer1.Enabled = false;
                    timer2.Enabled = false;
                    MessageBox.Show ("Time is over.");
                }
            }
            int hour = _nowSecond / 36000;
            int minute = (_nowSecond % 36000) / 600;
            double second = (_nowSecond % 36000) % 600 / 10.0;
            labelHour.Text = hour.ToString("00");
            labelMinute.Text = minute.ToString("00");
            labelSecond.Text = second.ToString("00.0");
            
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (labelColon1.Visible == true)
            {
                labelColon1.Visible = false;
                labelColon2.Visible = false;

            }
            else
            {
                labelColon1.Visible = true;
                labelColon2.Visible = true;

            }
        }

        private void buttonPauseContinue_Click(object sender, EventArgs e)
        {
            if (buttonPauseContinue.Text == "暂停") 
           {
                timer1.Enabled = false;
                timer2.Enabled = false;
                buttonPauseContinue.Text = "继续";
            }
            else
            {
                timer1.Enabled = true;
                timer2.Enabled = true;
                buttonPauseContinue.Text = "暂停";
            }

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            _nowSecond = 0;
            timer1.Enabled = false;
            timer2.Enabled = false;
            buttonStart.Enabled = true;
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            labelColon1.Visible = true;
            labelColon2.Visible = true;
            buttonPauseContinue.Text = "暂停";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            buttonStart.Enabled = true;
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            buttonPauseContinue.Text = "暂停";
            labelColon1.Visible = true;
            labelColon2.Visible = true;
        }

        private void Form1_Move(object sender, EventArgs e)
        {
            int screenRight = Screen.PrimaryScreen.Bounds.Right;
            int formRight = this.Left + this.Size.Width;
            if (Math.Abs(screenRight - formRight) <= 60)
                this.Left=screenRight-this.Size.Width;
            if (Math.Abs(this.Left) <= 60)
                this.Left=0;
            int screenBottom = Screen.PrimaryScreen.Bounds.Bottom;
            int formBottom = this.Top + this.Size.Height;
            if (Math.Abs(screenBottom - formBottom) <= 60)
                this.Top=screenBottom-this.Size.Height;
            if (Math.Abs(this.Top) <= 100)
                this.Top=0;
        }

        private void labelColon1_Click(object sender, EventArgs e)
        {

        }

    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyTimer
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

现存bug

应该目前为止我还没发现bug……但说不定某天 就突然出现了一个。。。
时间对不上(用久了就有误差,用很久就误差越来越大,原因似乎是?c#内部要干嘛干嘛的我也没听清楚,反正 就是会延迟i /氦/

发布了106 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44702847/article/details/103908252