Set start time for Stopwatch program in C#

后端 未结 5 2224
粉色の甜心
粉色の甜心 2021-01-06 09:29

I want to write a simple stopwatch program, I can make it work with the following code

    public Form1()
    {
        InitializeComponent();
    }
    Syst         


        
5条回答
  •  时光说笑
    2021-01-06 09:54

    Create a new instance of System.TimeSpan with the initial value of 45 minutes as per you example. Than add the value of the stopwatch to it TimeSpan.Add Method. Conveniently the Stopwatch.Elapsed is of type System.TimeSpan already.

    Than use string formatter to print the formatted time into the label. I think one of the other answers already shows how to do that. Otherwise here are some good references on how to format a TimeSpan instance TimeSpan.ToString Method (String) or using a custom formatter.

    var offsetTimeSpan = new System.TimeSpan(0,45,0).Add(ss.Elapsed)
    

提交回复
热议问题