首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何添加用于设置目标时间的窗体设计器控件?

如何添加用于设置目标时间的窗体设计器控件?
EN

Stack Overflow用户
提问于 2018-10-14 23:58:33
回答 1查看 67关注 0票数 0

当计时器快要停止的时候。

这个盒子是一个小的richTextBox,我在上面添加了一个小标签“设置时间目标”。

当计时器到达目标时间时,计时器正在向上或向下计数,停止一切。

我不确定是否使用richTextbox或textBox,以及如何使用户可以更改时间输入,然后在手表/计时器运行或不运行时生效,以便在到达目标时间时停止。

richTextBox/textBox中的格式应为00:00:00小时/分钟/秒

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using DannyGeneral;

namespace StopwatchTimer
{
    public partial class Form1 : Form
    {
        private static readonly Stopwatch watch = new Stopwatch();
        private long diff = 0, previousTicks = 0, ticksDisplayed = 0;
        private OptionsFile optionsfile = new OptionsFile(Path.GetDirectoryName(Application.LocalUserAppDataPath) + "\\Settings.txt");
        private string result;
        private bool runOnStart = false;
        private bool countingDown = false;

        public Form1()
        {
            InitializeComponent();

            richTextBox1.TabStop = false;
            richTextBox1.ReadOnly = true;
            richTextBox1.BackColor = Color.White;
            richTextBox1.Cursor = Cursors.Arrow;
            richTextBox1.Enter += RichTextBox1_Enter;

            trackBarHours.Value = Convert.ToInt32(optionsfile.GetKey("trackbarhours"));
            trackBarMinutes.Value = Convert.ToInt32(optionsfile.GetKey("trackbarminutes"));
            trackBarSeconds.Value = Convert.ToInt32(optionsfile.GetKey("trackbarseconds"));

            richTextBox1.Text = optionsfile.GetKey("result");

            TimeSpan ctimeSpan = new TimeSpan(0, trackBarHours.Value, trackBarMinutes.Value, trackBarSeconds.Value, 0);
            ticksDisplayed = ctimeSpan.Ticks;

            radioButton1.Checked = GetBool("radiobutton1");

            if (ticksDisplayed > 0 && radioButton1.Checked == false)
                radioButton2.Checked = true;

            if (ticksDisplayed == 0)
                radioButton1.Checked = true;

            if(trackBarHours.Value == 0 && trackBarMinutes.Value == 0 && trackBarSeconds.Value == 0)
            {
                btnPause.Enabled = false;
                btnReset.Enabled = false;
            }
            else
            {
                btnPause.Enabled = false;
                btnReset.Enabled = true;
            }

            runOnStart = GetBool("runonstart");
            if(runOnStart == true)
            {
                autoRunOnStart.Checked = true;
                StartOnRun();
            }
            else
            {
                autoRunOnStart.Checked = false;
            }
        }

        private void RichTextBox1_Enter(object sender, EventArgs e)
        {
            btnStart.Focus();
        }

        private void UpdateTime()
        {
            if (ticksDisplayed > 0)
                btnReset.Enabled = true;

            richTextBox1.Text = GetTimeString(watch.Elapsed);
            optionsfile.SetKey("result", result.ToString());
        }

        private string GetTimeString(TimeSpan elapsed)
        {
            result = string.Empty;

            //calculate difference in ticks
            diff = elapsed.Ticks - previousTicks;

            if (radioButton1.Checked == true)
            { //counting up
                ticksDisplayed += diff;
            }
            else
            {
                if (countingDown)
                {
                    ticksDisplayed += diff;
                }
                else
                {
                    //counting down
                    ticksDisplayed -= diff;
                }
            }

            if (ticksDisplayed < 0)
            {
                ticksDisplayed = 0;

                watch.Stop();
                btnStart.Text = "START";
                btnPause.Text = "PAUSE";
                btnPause.Enabled = false;
                if (trackBarHours.Value == 0 && trackBarMinutes.Value == 0 && trackBarSeconds.Value == 0 && ticksDisplayed == 0)
                {
                    btnReset.Enabled = false;
                }

                timer1.Enabled = false;
            }

            //Make ticksDisplayed to regular time to display in richtextbox
            TimeSpan ctimeSpan = new TimeSpan(ticksDisplayed);

            if (trackBarHours.Value != ctimeSpan.Hours) { trackBarHours.Value = ctimeSpan.Hours; }
            if (trackBarMinutes.Value != ctimeSpan.Minutes) { trackBarMinutes.Value = ctimeSpan.Minutes; }
            if (trackBarSeconds.Value != ctimeSpan.Seconds) { trackBarSeconds.Value = ctimeSpan.Seconds; }

            result = string.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                ctimeSpan.Hours,
                ctimeSpan.Minutes,
                ctimeSpan.Seconds,
                ctimeSpan.Milliseconds);

            previousTicks = elapsed.Ticks;

            return result;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "START")
            {
                watch.Reset();

                TimeSpan ctimeSpan = new TimeSpan(0, trackBarHours.Value, trackBarMinutes.Value, trackBarSeconds.Value, 0);
                diff = 0;
                previousTicks = 0;
                ticksDisplayed = ctimeSpan.Ticks;

                watch.Start();
                btnStart.Text = "STOP";
                btnPause.Enabled = true;
                btnReset.Enabled = true;
                timer1.Enabled = true;
            }
            else
            {
                watch.Stop();
                btnStart.Text = "START";
                btnPause.Text = "PAUSE";
                btnPause.Enabled = false;
                if (trackBarHours.Value == 0 && trackBarMinutes.Value == 0 && trackBarSeconds.Value == 0 && ticksDisplayed == 0)
                {
                    btnReset.Enabled = false;
                }
                else
                {
                    btnReset.Enabled = true;
                }

                if (ticksDisplayed > 0)
                    btnReset.Enabled = true;

                timer1.Enabled = false;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            watch.Reset();

            diff = 0;
            previousTicks = 0;
            ticksDisplayed = 0;
            trackBarHours.Value = 0;
            trackBarMinutes.Value = 0;
            trackBarSeconds.Value = 0;

            if (btnPause.Text == "PAUSE" && btnStart.Text == "STOP")
                watch.Start();

            if (trackBarHours.Value == 0 && trackBarMinutes.Value == 0 && trackBarSeconds.Value == 0)
            {
                btnReset.Enabled = false;
            }
            else
            {
                btnReset.Enabled = true;
            }

            if (radioButton2.Checked && ticksDisplayed == 0)
            {
                countingDown = true;
                radioButton2.Checked = false;
                radioButton1.Checked = true;
            }

            UpdateTime();
        }

        private void trackBarHours_Scroll(object sender, EventArgs e)
        {
            //get ticksDisplayed as TimeSpan
            TimeSpan ctimeSpan = new TimeSpan(ticksDisplayed);
            //change only the hour
            TimeSpan htimeSpan = new TimeSpan(ctimeSpan.Days, trackBarHours.Value, ctimeSpan.Minutes, ctimeSpan.Seconds, ctimeSpan.Milliseconds);

            //set it to ticksDisplayed and update.
            ticksDisplayed = htimeSpan.Ticks;

            TrackbarsScrollStates();

            optionsfile.SetKey("trackbarhours", trackBarHours.Value.ToString());

            UpdateTime();
        }

        private void trackBarMinutes_Scroll(object sender, EventArgs e)
        {
            TimeSpan ctimeSpan = new TimeSpan(ticksDisplayed);
            TimeSpan mtimeSpan = new TimeSpan(ctimeSpan.Days, ctimeSpan.Hours, trackBarMinutes.Value, ctimeSpan.Seconds, ctimeSpan.Milliseconds);

            ticksDisplayed = mtimeSpan.Ticks;

            TrackbarsScrollStates();

            optionsfile.SetKey("trackbarminutes", trackBarMinutes.Value.ToString());

            UpdateTime();
        }

        private void trackBarSeconds_Scroll(object sender, EventArgs e)
        {
            TimeSpan ctimeSpan = new TimeSpan(ticksDisplayed);
            TimeSpan stimeSpan = new TimeSpan(ctimeSpan.Days, ctimeSpan.Hours, ctimeSpan.Minutes, trackBarSeconds.Value, ctimeSpan.Milliseconds);

            ticksDisplayed = stimeSpan.Ticks;

            TrackbarsScrollStates();

            optionsfile.SetKey("trackbarseconds", trackBarSeconds.Value.ToString());

            UpdateTime();
        }

        private void TrackbarsScrollStates()
        {
            if (trackBarSeconds.Value == 0 && trackBarHours.Value == 0 && trackBarMinutes.Value == 0)
                btnReset.Enabled = false;
            if (trackBarSeconds.Value > 0 || trackBarHours.Value > 0 || trackBarMinutes.Value > 0)
                btnReset.Enabled = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            optionsfile.SetKey("radiobutton1", radioButton1.Checked.ToString());
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            optionsfile.SetKey("trackbarhours", trackBarHours.Value.ToString());
            optionsfile.SetKey("trackbarminutes", trackBarMinutes.Value.ToString());
            optionsfile.SetKey("trackbarseconds", trackBarSeconds.Value.ToString());
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "STOP")
            {
                if (btnPause.Text == "PAUSE")
                {
                    btnPause.Text = "CONTINUE";
                    watch.Stop();
                    timer1.Enabled = false;
                }
                else
                {
                    btnPause.Text = "PAUSE";
                    watch.Start();
                    timer1.Enabled = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateTime();
        }

        private void autoRunOnStart_CheckedChanged(object sender, EventArgs e)
        {
            if (autoRunOnStart.Checked)
            {
                runOnStart = true;
            }
            else
            {
                runOnStart = false;
            }

            optionsfile.SetKey("runonstart", runOnStart.ToString());
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            countingDown = false;
        }

        private void StartOnRun()
        {
            watch.Reset();

            TimeSpan ctimeSpan = new TimeSpan(0, trackBarHours.Value, trackBarMinutes.Value, trackBarSeconds.Value, 0);
            diff = 0;
            previousTicks = 0;
            ticksDisplayed = ctimeSpan.Ticks;

            watch.Start();
            btnStart.Text = "STOP";
            btnPause.Enabled = true;
            btnReset.Enabled = true;
            timer1.Enabled = true;
        }

        private bool GetBool(string keyname)
        {
            string radiobutton1 = optionsfile.GetKey(keyname);
            bool b;
            bool.TryParse(radiobutton1.Trim(), out b);

            return b;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-15 06:21:35

你不需要richtextbox。使用DateTimePicker。转到属性并设置:

格式:自定义

ShowUpDown : True

CustomFormat : HH:mm:ss

什么时候你想得到hourminutesseconds

代码语言:javascript
复制
int hour = dateTimePicker1.Value.Hour;
int minutes = dateTimePicker1.Value.Minute;
int seconds = dateTimePicker1.Value.Second;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52804495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档