A brief summary: I am very new to C# and this is my first time programming in this language. I have more experience with scripting languages. For a college assignment we must make an age calculator which calculates the age in days of the user, based on combo box input.
My question is, how (and where) do I implement leap year control within my code? I am aware of DateTime.IsLeapYear, however I am very unfamiliar with the syntax and do not know where to start with it.
I have added my code below so you can see my progress so far. I am working in Visual Studio 2017.
P.S. I must also implement a try/catch block somewhere, but as I said above I do not know where to start with it. Where would this go?
What I have tried:
public partial class Form1 : Form
private void Form1_Load(object sender, EventArgs e)
string monthArray = new string;
monthArray = "January";
monthArray = "February";
monthArray = "March";
monthArray = "April";
monthArray = "May";
monthArray = "June";
monthArray = "July";
monthArray = "August";
monthArray = "September";
monthArray = "October";
monthArray = "November";
monthArray = "December";
for (int index = 0; index < monthArray.Length; index++)
for (int i = 1900; i <= DateTime.Now.Year; i++)
for (int i = 1; i <= 31; i++)
private void timerDateTime_Tick(object sender, EventArgs e)
labelDate.Text = DateTime.Now.ToShortDateString();
labelTime.Text = DateTime.Now.ToLongTimeString();
private void comboBoxDay_SelectedIndexChanged(object sender, EventArgs e)
private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e)
private void buttonCalculate_Click(object sender, EventArgs e)
DateTime dob = new DateTime(Convert.ToInt32(comboBoxYear.SelectedItem), comboBoxMonth.SelectedIndex + 1, Convert.ToInt32(comboBoxDay.SelectedItem));
TimeSpan diff = DateTime.Now - dob;
int days = (int)diff.TotalDays;
MessageBox.Show(textBoxForename.Text + " is days old " + days.ToString());