C# - Simple Age Calculator

Submitted by: 
Language: 
Visitors have accessed this post 337 times.

In this tutorial we will create a Simple Age Calculator using C#. This code can be use when you are trying to guess a certain person age. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. It has a designed for improving productivity in the development of Web applications. It has a friendly environment for all new developers. So let's do the coding.

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want.

Here's the link for the Visual Studio https://www.visualstudio.com/.

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.

  1. namespace Simple_Age_Calculator
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
  32. this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
  33. this.label1 = new System.Windows.Forms.Label();
  34. this.label2 = new System.Windows.Forms.Label();
  35. this.btn_calculate = new System.Windows.Forms.Button();
  36. this.txt_result = new System.Windows.Forms.TextBox();
  37. this.btn_reset = new System.Windows.Forms.Button();
  38. this.SuspendLayout();
  39. //
  40. // dateTimePicker1
  41. //
  42. this.dateTimePicker1.CalendarFont = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  43. this.dateTimePicker1.CustomFormat = "MM/dd/yyyy";
  44. this.dateTimePicker1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  45. this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
  46. this.dateTimePicker1.Location = new System.Drawing.Point(128, 92);
  47. this.dateTimePicker1.Name = "dateTimePicker1";
  48. this.dateTimePicker1.Size = new System.Drawing.Size(439, 35);
  49. this.dateTimePicker1.TabIndex = 0;
  50. this.dateTimePicker1.Value = new System.DateTime(1965, 1, 1, 0, 0, 0, 0);
  51. //
  52. // dateTimePicker2
  53. //
  54. this.dateTimePicker2.CalendarFont = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  55. this.dateTimePicker2.CustomFormat = "MM/dd/yyyy";
  56. this.dateTimePicker2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  57. this.dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
  58. this.dateTimePicker2.Location = new System.Drawing.Point(128, 206);
  59. this.dateTimePicker2.Name = "dateTimePicker2";
  60. this.dateTimePicker2.Size = new System.Drawing.Size(439, 35);
  61. this.dateTimePicker2.TabIndex = 1;
  62. //
  63. // label1
  64. //
  65. this.label1.AutoSize = true;
  66. this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  67. this.label1.Location = new System.Drawing.Point(128, 39);
  68. this.label1.Name = "label1";
  69. this.label1.Size = new System.Drawing.Size(82, 27);
  70. this.label1.TabIndex = 2;
  71. this.label1.Text = "FROM";
  72. //
  73. // label2
  74. //
  75. this.label2.AutoSize = true;
  76. this.label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  77. this.label2.Location = new System.Drawing.Point(128, 166);
  78. this.label2.Name = "label2";
  79. this.label2.Size = new System.Drawing.Size(45, 27);
  80. this.label2.TabIndex = 3;
  81. this.label2.Text = "TO";
  82. //
  83. // btn_calculate
  84. //
  85. this.btn_calculate.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  86. this.btn_calculate.Location = new System.Drawing.Point(257, 359);
  87. this.btn_calculate.Name = "btn_calculate";
  88. this.btn_calculate.Size = new System.Drawing.Size(172, 39);
  89. this.btn_calculate.TabIndex = 4;
  90. this.btn_calculate.Text = "Calculate";
  91. this.btn_calculate.UseVisualStyleBackColor = true;
  92. this.btn_calculate.Click += new System.EventHandler(this.CalculateAge);
  93. //
  94. // txt_result
  95. //
  96. this.txt_result.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  97. this.txt_result.Location = new System.Drawing.Point(218, 283);
  98. this.txt_result.Name = "txt_result";
  99. this.txt_result.ReadOnly = true;
  100. this.txt_result.Size = new System.Drawing.Size(255, 35);
  101. this.txt_result.TabIndex = 5;
  102. this.txt_result.Text = "Result Here...";
  103. this.txt_result.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
  104. //
  105. // btn_reset
  106. //
  107. this.btn_reset.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  108. this.btn_reset.Location = new System.Drawing.Point(257, 418);
  109. this.btn_reset.Name = "btn_reset";
  110. this.btn_reset.Size = new System.Drawing.Size(172, 37);
  111. this.btn_reset.TabIndex = 6;
  112. this.btn_reset.Text = "Reset";
  113. this.btn_reset.UseVisualStyleBackColor = true;
  114. this.btn_reset.Click += new System.EventHandler(this.ResetResult);
  115. //
  116. // Form1
  117. //
  118. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  119. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  120. this.ClientSize = new System.Drawing.Size(683, 467);
  121. this.Controls.Add(this.btn_reset);
  122. this.Controls.Add(this.txt_result);
  123. this.Controls.Add(this.btn_calculate);
  124. this.Controls.Add(this.label2);
  125. this.Controls.Add(this.label1);
  126. this.Controls.Add(this.dateTimePicker2);
  127. this.Controls.Add(this.dateTimePicker1);
  128. this.Name = "Form1";
  129. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  130. this.Text = "Main";
  131. this.ResumeLayout(false);
  132. this.PerformLayout();
  133.  
  134. }
  135.  
  136. #endregion
  137.  
  138. private System.Windows.Forms.DateTimePicker dateTimePicker1;
  139. private System.Windows.Forms.DateTimePicker dateTimePicker2;
  140. private System.Windows.Forms.Label label1;
  141. private System.Windows.Forms.Label label2;
  142. private System.Windows.Forms.Button btn_calculate;
  143. private System.Windows.Forms.TextBox txt_result;
  144. private System.Windows.Forms.Button btn_reset;
  145. }
  146. }

or also you create the layout by dragging the designating tools to the forms.

Creating the Script

This code contains the function of the application. This code will try to calculate the given date to generate a proper result for the age. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Simple_Age_Calculator
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void CalculateAge(object sender, EventArgs e) {
  21. txt_result.Text = GenerateAge(dateTimePicker1.Value, dateTimePicker2.Value).ToString();
  22. }
  23.  
  24.  
  25. public long GenerateAge(System.DateTime start, System.DateTime end) {
  26. long age = 0;
  27. System.TimeSpan timeSpan = new TimeSpan(end.Ticks - start.Ticks);
  28. age = (long)(timeSpan.Days / 365);
  29.  
  30. return age;
  31. }
  32.  
  33. private void ResetResult(object sender, EventArgs e) {
  34. txt_result.Text = "Result Here...";
  35. }
  36. }
  37. }

Try to run the application and see if it works.

There you go we successfully create a Simple Age Calculator using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.