Create An Age Calculator Using C# Application

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.
  1. private void btnCalculateAge_Click(object sender, EventArgs e) {  
  2.     DateTime dateOfBirth;  
  3.     DateTime.TryParse(txtDOB.Text, out dateOfBirth);  
  4.     DateTime currentDate = DateTime.Now;  
  5.     TimeSpan difference = currentDate.Subtract(dateOfBirth);  
  6.     DateTime age = DateTime.MinValue + difference;  
  7.     int ageInYears = age.Year - 1;  
  8.     int ageInMonths = age.Month - 1;  
  9.     int ageInDays = age.Day - 1;  
  10.     txtAgeInYear.Text = (ageInYears).ToString();  
  11.     txtAgeInMonth.Text = (ageInMonths).ToString();  
  12.     txtAgeInDays.Text = (ageInDays).ToString();  
  13. }  
  14. private void btnCalculateAge_MouseMove(object sender, MouseEventArgs e) {  
  15.     toolTip1.SetToolTip(this.btnCalculateAge, "Enter to Calculate Your Age");  
  16. }  
  17. private void txtDOB_MouseHover(object sender, EventArgs e) {  
  18.     toolTip1.SetToolTip(this.txtDOB, "Enter your Birth Date");  
  19. }  
  20. private void btnExit_Click(object sender, EventArgs e) {  
  21.     Application.Exit();  
  22. }  
  23. private void Form1_FormClosing(object sender, FormClosingEventArgs e) {  
  24.     DialogResult result;  
  25.     result = MessageBox.Show("Do you realy want to close""Confirmation", MessageBoxButtons.YesNo);  
  26.     if (result == DialogResult.No) {  
  27.         e.Cancel = true;  
  28.     }  
  29. }  
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
Ebook Download
View all
Learn
View all
Rotary International is an international service organization whose stated purpose is to bring toget