Age Calculator Using Form Application In Visual Studio 2017

INTRODUCTION

In this article, I am going to explain how to calculate the present age of an individual using form application in Visual Studio 2017. Usually, Button controls are used for calculating age but here I am going to use Date Time Picker control to calculate Age.

STEP1 - Start the Project

Once Visual Studio is launched, in the menu option, choose a new project (New-->new project) and after that choose Visual C# and Windows Form application. Location, Solution, Solution Name, and Framework are to be kept the default. The name of the project can be given to you like (Age Calculator for this Project) Click ok to start the project.

UWP

STEP 2 - Properties of Windows Form

Form 1 is opened as Default. In this Form you can see as Designer view, it is a visual representation of the window that will open when your application is opened. You can switch between this designer form and Code view at any time by right-clicking the design surface of code window and then clicking View Code or View Designer.

In the form designer page, change the text of the Form as the Age Calculator and add an icon if needed. Since it is a calculator for calculating the age so it should appear in the center of the window. So in the property of the form, start position is changed to centralized  and its size should be fixed so that the maximize size is ser to false.

UWP
 
STEP 3 - Uses of DateTimePicker

Now, in the toolbox choose DateTimePicker.Drag and Drop it into the Form.

The Windows Forms DateTimePicker control  is used to select a single item from a list of dates. It is used to represent a date, the following two will appear in DateTime Picker,
  1. A drop-down list with a date represented in text
  2. A grid that appears when you click on the down-arrow next to the list. The grid looks like the MonthCalendar control, which can be used for selecting multiple dates.
UWP

STEP 4 - Control Functions

Drag and Drop Button, Labels, Date Time Picker and Textbox. Here in this Project, I used one button, three Labels, Textbox, Two DateTimePicker. 

As below, three Labels are used as Birth, Present and Age. Button to Calculate the Age. Inputs are chosen from the DateTimePicker and the Output obtained in the Textbox.

UWP

STEP 5 - Coding

When the Button is pressed, the output age of an individual is Displayed on the Textbox. Date Time Picker is used as input of this project.
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     DateTime startdate = dateTimePicker1.Value;  
  4.     DateTime enddate = dateTimePicker2.Value;  
  5.     textBox1.Text = calcage(startdate,enddate).ToString();  
  6. }  
  7.   
  8.     public long calcage(System.DateTime Startdate,System.DateTime EndDate)  
  9. {  
  10.     long age = 0;  
  11.     System.TimeSpan ts = new TimeSpan(Startdate.Ticks - EndDate.Ticks);  
  12.     age = (long)(ts.Days / 365);  
  13.     return age;  
  14. }  
UWP

STEP 6 - Output of The Project

Choose your Birthday Date in the first Date Time Picker and the Present Date in the next Date Time Picker, now Click Calculate Button and then the output is obtained in the Textbox.

I chose,

Birth=08/05/2010
Present=08/05/2018
Age=8 

UWP
 
Summary

Hope this article for calculating an age using DateTime Picker in form Application will replace the old method of calculation using Buttons and Textbox. It is simple to implement. 


Similar Articles