Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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();


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
Posted
Updated 20-Jan-11 16:11pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 22:22pm    
Wow!
Do you want to talk about this?
:-)
Sandeep Mewara 21-Jan-11 0:41am    
No idea why downvoted. Countered!

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);
 
Share this answer
 
v2
Comments
Nish Nishant 20-Jan-11 22:17pm    
Posted it before I did, voted 5, proposed as answer.
Yusuf 20-Jan-11 22:42pm    
Thank you. I was going to propose passing in the constructor, but did not feel the OP may get it, do when for property instead.
Gerard Delower 20-Jan-11 23:10pm    
Thanks Nishant and Yusuf for your suggestions, my span.days is a local variable in the program.cs when I declare "form1.DaysSpan = span.days;" it gives an error of course as it cant see the variable. Also how do I set the text for the label control as it's done in the form1 design window in the properties panel - programatically cheers Dene
Yusuf 21-Jan-11 9:56am    
Do you have DaysSpan defined on your Form1?
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.
 
Share this answer
 
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()));


Hope you are celar now

Happy Programming!!!! :)

Regards
Vipin Kumar Mallaya
 
Share this answer
 
Comments
Gerard Delower 20-Jan-11 23:12pm    
Thanks Vipin, all I want is to display the variables value in the text form the label, the variable is in the program.cs
cheers Dene
Gerard Delower 23-Jan-11 20:45pm    
Hi All I worked out how to concatinate strings and variables in my form.cs
label1.Text = " Your machine has been up for " + span.Days + " Days A reboot is recomended";
I cheated by putting the the wmi call function in the form.cs ,now as a learning process I want to pass the variable span.days from my program.cs to the form.cs and not have the function twice
cheers Dene

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900