In this article, I am going to show you a simple Age Calcualtor, using C# Application. Here, you are able to calculate your age, using C# Windows Application code.
This is not a perfect Age Calculator but you are able to perform most of the operations like calculating your current age. Please follow the steps given below to create an Age calculator, using C# Application.
Step 1
Open your Visual Studio, create a New Project and name it Age Calculator.
Step 2
Click OK button, which redirects you to the Next Page, where you are able to create a page.
Step 3
Change the form text property of an Age Calculator, because we don’t want our Application to have the title Form1 when it starts.
Step 4
Design your page accordingly and add a tooltip in the page given below.
Step 5
Change the button name and change all textBox names. Afterwards, double click on the button and write down the code.
- private void btnCalculateAge_Click(object sender, EventArgs e) {
- DateTime dateOfBirth;
- DateTime.TryParse(txtDOB.Text, out dateOfBirth);
- DateTime currentDate = DateTime.Now;
- TimeSpan difference = currentDate.Subtract(dateOfBirth);
- DateTime age = DateTime.MinValue + difference;
- int ageInYears = age.Year - 1;
- int ageInMonths = age.Month - 1;
- int ageInDays = age.Day - 1;
- txtAgeInYear.Text = (ageInYears).ToString();
- txtAgeInMonth.Text = (ageInMonths).ToString();
- txtAgeInDays.Text = (ageInDays).ToString();
- }
- private void btnCalculateAge_MouseMove(object sender, MouseEventArgs e) {
- toolTip1.SetToolTip(this.btnCalculateAge, "Enter to Calculate Your Age");
- }
- private void txtDOB_MouseHover(object sender, EventArgs e) {
- toolTip1.SetToolTip(this.txtDOB, "Enter your Birth Date");
- }
- private void btnExit_Click(object sender, EventArgs e) {
- Application.Exit();
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
- DialogResult result;
- result = MessageBox.Show("Do you realy want to close", "Confirmation", MessageBoxButtons.YesNo);
- if (result == DialogResult.No) {
- e.Cancel = true;
- }
- }
Once you enter the Date of Birth, It will show you the current age
Example : My Date of Birth : 03/06/1991
Output: 25 years 11 months 13 days