Hi,
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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NumberOfDaysOld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] monthArray = new string[12];
monthArray[0] = "January";
monthArray[1] = "February";
monthArray[2] = "March";
monthArray[3] = "April";
monthArray[4] = "May";
monthArray[5] = "June";
monthArray[6] = "July";
monthArray[7] = "August";
monthArray[8] = "September";
monthArray[9] = "October";
monthArray[10] = "November";
monthArray[11] = "December";
for (int index = 0; index < monthArray.Length; index++)
{
comboBoxMonth.Items.Add(monthArray[index]);
}
for (int i = 1900; i <= DateTime.Now.Year; i++)
{
comboBoxYear.Items.Add(i.ToString());
}
for (int i = 1; i <= 31; i++)
{
comboBoxDay.Items.Add(i.ToString());
}
}
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());
}
}
}
}