在文本内标记控制变量 [英] label control variables inside the text

查看:86
本文介绍了在文本内标记控制变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
刚刚开始使用和学习视觉C#-我有一个正在开发的小应用程序
-正常运行时间/重启提示.我可以使用它,但是我想显示自上次重新启动以来的天数
变量"在Form.cs的标签文本控件中.

这是标签中的文本"
自上述日期以来,该机器尚未重新启动
建议重新启动!

获取自上次重新启动以来的天数的代码.

Hello All
Just started using and learning visual c# - again , I have a little app I am working on
- an uptime/reboot reminder. I have it working but I want to display the "number of days since the last reboot
variable" in the label text control in the Form.cs.

"This is the text in the label"
This machine has not been restarted since the date above
A Reboot is recommended !

Code for getting the number of days since the last reboot.

// define a select query
            SelectQuery query = 
                new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary=''true''");
            
            // create a new management object searcher and pass it
            // the select query
            ManagementObjectSearcher searcher = 
                new ManagementObjectSearcher(query);
            // get the datetime value and set the local boot
            // time variable to contain that value
            foreach(ManagementObject mo in searcher.Get())
            {
                BootTime =
                ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
            }
            TimeSpan span = NowTime.Subtract(BootTime);
            if (span.Days > 5)
            {
                Application.Run(new Form1());
            }
            else Application.Exit();



这是在我的Program.cs中;可以在标签文本控件文本中的Form.cs中引用变量(Span.days)吗?


非常感谢-Dene



This is in my Program.cs; can I reference the variable (Span.days) in my Form.cs in the label text control- text?


Many thanks - Dene

推荐答案

在Form1中设置属性

Set a property in your Form1

public int DaysSpan{ get; set; }



那么你可以打电话



then you can call

Form1 form = new Form1();
form.DaysSpan = span.Days;
Application.Run(form);


您可以通过构造函数或form属性将TimeSpan 对象传递给表单.在表单的加载事件中,您可以将标签的文本设置为所需的值.
You could pass the TimeSpan object to the form either via the constructor or via a form property. And in the form''s load event, you can set the label''s text to the required value.


亲爱的朋友
由于我们需要显示上次重新启动的日期(希望不是天数),因此需要从当前日期中减去时间跨度(span变量),并在标签中显示日期

下面的代码将给您一个想法
Dear Friend
As we need to show the last reebooted date(hope not days) we need to subtract the time span(span variable) from the current date and display the date in the label

Below code will give you an idea
 DateTime dt1;
 DateTime dt2;
 dt1=DateTime.Now;
 dt2=DateTime.Now.AddDays(5);//a sample date

 TimeSpan span = dt2.Subtract(dt1);
 dt1=dt1.Subtract(span);
//set the value in dt1 to label
 MessageBox.Show((dt1.ToString()));



希望你现在是天青星

编程愉快!!! :)

问候
Vipin Kumar Mallaya



Hope you are celar now

Happy Programming!!!! :)

Regards
Vipin Kumar Mallaya


这篇关于在文本内标记控制变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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