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

时间:2014-09-26 17:14:54

标签: c# windows-phone-8 time stopwatch

我想弄清楚如何设置小时,分钟,秒和&从文本框到长变量的毫秒值,以便我可以操作应用程序中的时间值。

到目前为止,我所尝试的是创建一个名为' 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();

            }



        }

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

//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)