adding hours from label and properties.system.defa

2019-09-20 02:49发布

问题:

i have maded a counter which is a simple counter which starts from 00:00:00 and incremented in every seconds i will provide my code for that

in the form load event i have written this

 private DateTime startTime; 
        private void Form7_Load(object sender, EventArgs e)
        {
            startTime = DateTime.Now; 
            timer1.Start();

        }

 private void timer1_Tick(object sender, System.EventArgs e)
            {

                counter_label.Text = (DateTime.Now - startTime)
.ToString(@"hh\:mm\:ss");
            }

the timer is set to 1000 means to 1 seconds so my timer works fine but now i want to save the label.text to the setting of the winform which is located in the properties.settings.default so i saved the text to that by

private void Form7_FormClosing(object sender, FormClosingEventArgs e)
        {

            DateTime st;
            DateTime end;
            st = Convert.ToDateTime(Properties.Settings.Default.datetime);
            end = Convert.ToDateTime(counter_label.Text);

            Properties.Settings.Default.datetime = counter_label.Text;
          total_label.Text = (st + end ).ToString(@"hh\:mm\:ss");// this is not happening

          Properties.Settings.Default.datetime = total_label.Text;
            Properties.Settings.Default.Save();
        }

the error is operand + cannot be applied to system.datetime and system.datetime my intention is. when i save the text to the system properties. at the form closing event, the new updated result must be saved which is. the old text from the system.properties and the new fromt the counter_label. how can it be done?

回答1:

Do not use setting of string type to store TimeSpan object (as Habib already stated, subtraction or addition of DateTime objects produces TimeSpan). You should create settings of type System.TimeSpan in Settings.settings file (e.g. with name TotalTime).

Saving:

Properties.Settings.Default.TotalTime = (DateTime.Now - startTime);
Properties.Settings.Default.Save();

Loading (here is trick - you are subtracting time which left from current time. So, if you have two minutes left, start time will be set for two minutes before you started applicatioin, like you never closed it):

startTime = DateTime.Now.Subtract(Properties.Settings.Default.TotalTime);

So, you don't need parsing any strings. Work with DateTime and TimeSpan, and let .NET to do all persisting work for you.


UPDATE (all code):

DateTime startTime;

private void timer1_Tick(object sender, EventArgs e)
{
    label1.Text = (DateTime.Now - startTime).ToString(@"hh\:mm\:ss");
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    timer1.Stop();
    Properties.Settings.Default.TotalTime = (DateTime.Now - startTime);
    Properties.Settings.Default.Save();
}

private void Form1_Load(object sender, EventArgs e)
{
    startTime = DateTime.Now.Subtract(Properties.Settings.Default.TotalTime);
    timer1.Start();
}

If it is first run of your application, then Properties.Settings.Default.TotalTime will return default value of TimeSpan (which is zero time span). So, subtracting it from current time will return same value as current time.



回答2:

You can't add Two date time objects. I believe your counter_label has a TimeSpan value, since its format is 00:00:00, You can parse that to TimeSpan and then add that to the your st DateTime type object.

TimeSpan ts = TimeSpan.Parse(counter_label.Text);
total_label.Text = st.Add(ts).ToString(@"hh\:mm\:ss");


回答3:

The operator + define like this :

public static DateTime operator +(
    DateTime d,
    TimeSpan t
)

Adds a specified time interval to a specified date and time, yielding a new date and time. *you can add(+) timeSpand to dateTime only!!!!* Fix Code:

TimeSpan sp = TimeSpan.Parse(counter_label.Text);
total_label.Text = (st+sp).ToString(@"hh\:mm\:ss");


回答4:

The difference between two DateTime is not another DateTime, but a TimeSpan. So what you want to persist is not a moment in time, but the total running time, which should be a TimeSpan.