Tracking Time in a Window Application


Overview

My youngest son loves numbers and statistics. He often asks "How many seconds old am I?" or "How old is Mama in minutes?". I decided to write a program for him that would display the ages of everyone in our family in seconds, minutes, hours, days, months, and years. It also displays some summary information, such as average age, relative age, and how many days until the next birthday.

The program reads from an XML file to get the data about names and birth dates, so it is easy to add or remove names without changing the code in case my son decides the program should include his pet dog or turtle.

Retrieving Names and Birthdates

The first thing the program does is fill a string array "Names" and a DateTime array "BirthDate" with the data from the XML file.

private void Load_dsBDays()
{
DataSet dsBDays =
new DataSet();
dsBDays.ReadXml(Environment.CurrentDirectory + \\BDays.xml);
BirthDate =
new DateTime[dsBDays.Tables[0].Rows.Count];
Names =
new string[dsBDays.Tables[0].Rows.Count];
for (int i=0; i<dsBDays.Tables[0].Rows.Count; i++)
{
DataRow dr = dsBDays.Tables[0].Rows[i];
BirthDate[i] = Convert.ToDateTime(dr["BDay"].ToString());
Names[i] = dr["Name"].ToString();
}
}

Set DataGrid Style

Next the program sets the style for the datagrid. The sets the color, width, dataformat, etc. of the datagrid and each of the columns in the datagrid. The code below shows how the first two columns are formatted.

DataGridTextBoxColumn col0 = new DataGridTextBoxColumn();
col0.TextBox.Enabled =
false;
col0.Alignment = HorizontalAlignment.Left;
col0.Width = 75;
col0.HeaderText = "Name";
col0.MappingName = "Name";
dgts.GridColumnStyles.Add(col0);
DataGridTextBoxColumn col1 =
new DataGridTextBoxColumn();
col1.TextBox.Enabled =
false;
col1.Alignment = HorizontalAlignment.Right;
col1.Format = "n0";
col1.Width = 85;
col1.HeaderText = "Seconds";
col1.MappingName = "Seconds";
dgts.GridColumnStyles.Add(col1);

Calculating Age Information

The main task of the program is to calculate the age information. The DataGrid that has all of the age information is bound to a DataSet that holds all of the calculations. When the DataSet changes, these changes automatically appear in the DataGrid. The calculations start from two pieces of information: the birth dates from the XML file and the current date and time. The basic second, minute, hour, day, etc. information is calculated in a separate class "TimeSeg" shown below.

using System;
namespace Ages
{
/// <summary>
/// Summary description for TimeSeg.
/// </summary>
public class TimeSeg
{
private DateTime dtime;
private double seconds;
private double minutes;
private double hours;
private double days;
private double years;
private double months;
private double nextBDayy;
private DateTime nextBDayi;
private string nextBDay;
public TimeSeg(DateTime pDTime)
{
this.dtime = pDTime;
this.seconds = Convert.ToDouble((DateTime.Now.Ticks - dtime.Ticks) / 10000000);
this.minutes = seconds / 60.0;
this.hours = minutes / 60.0;
this.days = hours / 24.0;
this.years = days / 365.25;
this.months = years * 12;
this.nextBDayy = Math.Floor(years) + 1.0;
this.nextBDayi = dtime.AddYears(Convert.ToInt32(nextBDayy));
this.nextBDay = (nextBDayi.Date - DateTime.Now.Date).Days.ToString() + " Days";
}
public double Seconds { get {return seconds;} }
public double Minutes { get {return minutes;} }
public double Hours { get {return hours;} }
public double Days { get {return days;} }
public double Years { get {return years;} }
p
ublic double Months { get {return months;} }
public string NextBDay { get {return nextBDay;} }
}
}

Tick Event

A timer set to go off every second calls the method "CalculateAges()" that get the new age data that is calculated from everyone's birth date and the current time.

SelectedIndexChanged Event

When the ComboBox filled with the array "Names", which has the names of everyone in the XML file, is changed, it sets the relative age of everyone to the person who was selected. The relative age indicates how many years older or younger everyone is compared to the person selected both in years and percentage.

Conclusion

There are many ways this application could be modified. For the time being it appears to have satisfied my son's desire to know the various time segments of how long we have been alive. Every few days he checks to see how everyone's age is progressing. I feel like mine is progressing too rapidly.