繁体   English   中英

如何将文本框中的时间值分配给变量?

[英]How to assign a time value from a textbox to a variable?

我试图弄清楚如何将文本框中的小时,分​​钟,秒和毫秒值设置为长变量,以便可以在应用程序中操作时间值。

到目前为止,我一直在尝试创建一个名为“ workTime”的长变量,但是我不确定如何将在文本框中输入的值分配为一段时间。

例如,如果用户输入“ 00:02:30:000”,我该如何将其分配给变量?

是否可以通过这种方式分配时间值?

任何建议或替代方法将不胜感激。

private void startBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {


            string wrkString;
            string rstString; 
            int i;

            //Assign text box time string to string variables.
            wrkString = wrkTbx.Text;
            rstString = restTbx.Text;


            //Assign text box string value to a date time variable.

            DateTime workDt = DateTime.ParseExact(wrkString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);
            DateTime restDt = DateTime.ParseExact(rstString.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);


            StopGoCvs.Background = new SolidColorBrush(Colors.Green);
            hornElmt.Play();
            //    // set up the timer
            myTimer = new DispatcherTimer();
            myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
            myTimer.Tick += myTimer_Tick;

            //tell timer to stop when it has reached the allocated work time.
            if(myTimer.Interval != workDt.TimeOfDay)
            {
                // start both timers
                myTimer.Start();
                myStopwatch.Start();

            }
            else
            {
                myTimer.Stop();
                myStopwatch.Stop();

            }



        }

在此处输入图片说明

尝试这个:

DateTime dt = DateTime.ParseExact(time.Replace(": ", ":").Replace(" :", ":"), "HH:mm:ss:fff", CultureInfo.InvariantCulture);
//using System.Globalization

TimeSpan.ParseExact("00 : 02 : 30 : 000", @"%h\ \:\ %m\ \:\ %s\ \:\ %fff", CultureInfo.InvariantCulture.DateTimeFormat)
//from here you can decide to store the timespan value or the miliseconds via its 'TotalMilliseconds' property.


//It may help with readability by removing the space from the input
TimeSpan.ParseExact("00 : 02 : 30 : 000".Replace(" ", ""), @"%h\:%m\:%s\:%fff", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM