从 label 和 properties.system.default 添加小时数 [英] adding hours from label and properties.system.default

查看:27
本文介绍了从 label 和 properties.system.default 添加小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个计数器,它是一个简单的计数器,从 00:00:00 开始,每秒钟递增一次我会为此提供我的代码

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");
            }

计时器设置为 1000 意味着 1 秒,所以我的计时器工作正常但现在我想将 label.text 保存到位于 properties.settings.default 中的 winform 的设置所以我将文本保存到

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();
        }

错误是操作数 + 不能应用于 system.datetime 和 system.datetime我的意图是.当我将文本保存到系统属性时.在表单关闭事件中,必须保存新的更新结果,即.来自 system.properties 的旧文本和来自 counter_label 的新文本.怎么办?

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?

推荐答案

不要使用 string 类型的设置来存储 TimeSpan 对象(正如 Habib 已经说过的,减法或添加 DateTime 对象产生 TimeSpan).您应该在 Settings.settings 文件中创建 System.TimeSpan 类型的设置(例如名称为 TotalTime).

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

保存:

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

因此,您不需要解析任何字符串.使用 DateTime 和 TimeSpan,让 .NET 为您完成所有持久化工作.

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

更新(所有代码):

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();
}

如果是第一次运行您的应用程序,则 Properties.Settings.Default.TotalTime 将返回 TimeSpan 的默认值(零时间跨度).因此,从当前时间减去它会返回与当前时间相同的值.

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.

这篇关于从 label 和 properties.system.default 添加小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆