Age calculation in combo box fetching in C# Windows Application
Dear friends this is critical problem to mange date and calculation age of any person
thus i develop a code to solve this problem.who is interested take use easily.
Following Steps Follows:-
1.Create three combo box for date,month and year.
2.Then create function for maintain date and a year table of database bind with year combo box.
3.Then create four list of date with 28,29,30,31 days in method.
4.Then create a list of month in month combo box.
5.After then a switch case statement use for show listed in different types days in combo box so it is with changed event of year and month.
6.If we want to calculating Age then following Code See:-
private void button1_Click(object sender, EventArgs e)
{
string mth;
double age;
year = Convert.ToInt32(comboBox7.Text);
mth = comboBox6.SelectedValue.ToString();
string a = comboBox7.Text + "." + mth;
double op = Convert.ToDouble(a);
age =2011.7-op ;
textBox1.Text = age .ToString ();
}
Thus you calculate age of any person easily .
Here Example:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Date_of_Birth
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Enterprises;Integrated Security=True");
static SqlDataAdapter ad1 = new SqlDataAdapter();
private BindingSource bindingSource1 = new BindingSource();
private void GetData(string selectCommand)
{
try
{
ad1 = new SqlDataAdapter(selectCommand, con);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ad1);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
ad1.Fill(table);
bindingSource1.DataSource = table;
comboBox6.DisplayMember = "Month";
comboBox6.ValueMember = "id";
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " + "\n" +
"ConnectionString variable with a connection string that is " +
"valid for your system.");
}
}
public Form1()
{
InitializeComponent();
}
int year;
private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
{
year = Convert.ToInt32(comboBox7.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Load += new System.EventHandler(Form1_Load);
comboBox6.DataSource = bindingSource1;
GetData("Select * From Month ");//where ((Name='" + textBox1.Text + "')AND(Phone_No='" + textBox2.Text + "'))");
}
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox7.Text.Length < 2)
{
comboBox7.Focus();
}
else if (comboBox6.Text.Length < 2)
{
MessageBox.Show("Select Month");
comboBox6.Focus();
}
else
{
string month = comboBox6.Text;
year = Convert.ToInt32(comboBox7.Text);
switch (month)
{
case "January":
comboBox6.SelectedValue = "1";
fullDay();
break;
case "February":
if (year % 4 == 0)
{
febFullDay();
}
else
{
febShortDay();
}
break;
case "March":
fullDay();
break;
case "April":
shortDay();
break;
case "May":
fullDay();
break;
case "June":
shortDay();
break;
case "July":
fullDay();
break;
case "August":
fullDay();
break;
case "September":
shortDay();
break;
case "October":
fullDay();
break;
case "November":
shortDay();
break;
case "December":
fullDay();
break;
}
}
}
private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
}
void febShortDay()
{
comboBox5.Items.Add("1");
comboBox5.Items.Add("2");
comboBox5.Items.Add("3");
comboBox5.Items.Add("4");
comboBox5.Items.Add("5");
comboBox5.Items.Add("6");
comboBox5.Items.Add("7");
comboBox5.Items.Add("8");
comboBox5.Items.Add("9");
comboBox5.Items.Add("10");
comboBox5.Items.Add("11");
comboBox5.Items.Add("12");
comboBox5.Items.Add("13");
comboBox5.Items.Add("14");
comboBox5.Items.Add("15");
comboBox5.Items.Add("16");
comboBox5.Items.Add("17");
comboBox5.Items.Add("18");
comboBox5.Items.Add("19");
comboBox5.Items.Add("20");
comboBox5.Items.Add("21");
comboBox5.Items.Add("22");
comboBox5.Items.Add("23");
comboBox5.Items.Add("24");
comboBox5.Items.Add("25");
comboBox5.Items.Add("26");
comboBox5.Items.Add("27");
comboBox5.Items.Add("28");
}
void febFullDay()
{
comboBox5.Items.Add("1");
comboBox5.Items.Add("2");
comboBox5.Items.Add("3");
comboBox5.Items.Add("4");
comboBox5.Items.Add("5");
comboBox5.Items.Add("6");
comboBox5.Items.Add("7");
comboBox5.Items.Add("8");
comboBox5.Items.Add("9");
comboBox5.Items.Add("10");
comboBox5.Items.Add("11");
comboBox5.Items.Add("12");
comboBox5.Items.Add("13");
comboBox5.Items.Add("14");
comboBox5.Items.Add("15");
comboBox5.Items.Add("16");
comboBox5.Items.Add("17");
comboBox5.Items.Add("18");
comboBox5.Items.Add("19");
comboBox5.Items.Add("20");
comboBox5.Items.Add("21");
comboBox5.Items.Add("22");
comboBox5.Items.Add("23");
comboBox5.Items.Add("24");
comboBox5.Items.Add("25");
comboBox5.Items.Add("26");
comboBox5.Items.Add("27");
comboBox5.Items.Add("28");
comboBox5.Items.Add("29");
}
void shortDay()
{
comboBox5.Items.Add("1");
comboBox5.Items.Add("2");
comboBox5.Items.Add("3");
comboBox5.Items.Add("4");
comboBox5.Items.Add("5");
comboBox5.Items.Add("6");
comboBox5.Items.Add("7");
comboBox5.Items.Add("8");
comboBox5.Items.Add("9");
comboBox5.Items.Add("10");
comboBox5.Items.Add("11");
comboBox5.Items.Add("12");
comboBox5.Items.Add("13");
comboBox5.Items.Add("14");
comboBox5.Items.Add("15");
comboBox5.Items.Add("16");
comboBox5.Items.Add("17");
comboBox5.Items.Add("18");
comboBox5.Items.Add("19");
comboBox5.Items.Add("20");
comboBox5.Items.Add("21");
comboBox5.Items.Add("22");
comboBox5.Items.Add("23");
comboBox5.Items.Add("24");
comboBox5.Items.Add("25");
comboBox5.Items.Add("26");
comboBox5.Items.Add("27");
comboBox5.Items.Add("28");
comboBox5.Items.Add("29");
comboBox5.Items.Add("30");
}
void fullDay()
{
comboBox5.Items.Add("1");
comboBox5.Items.Add("2");
comboBox5.Items.Add("3");
comboBox5.Items.Add("4");
comboBox5.Items.Add("5");
comboBox5.Items.Add("6");
comboBox5.Items.Add("7");
comboBox5.Items.Add("8");
comboBox5.Items.Add("9");
comboBox5.Items.Add("10");
comboBox5.Items.Add("11");
comboBox5.Items.Add("12");
comboBox5.Items.Add("13");
comboBox5.Items.Add("14");
comboBox5.Items.Add("15");
comboBox5.Items.Add("16");
comboBox5.Items.Add("17");
comboBox5.Items.Add("18");
comboBox5.Items.Add("19");
comboBox5.Items.Add("20");
comboBox5.Items.Add("21");
comboBox5.Items.Add("22");
comboBox5.Items.Add("23");
comboBox5.Items.Add("24");
comboBox5.Items.Add("25");
comboBox5.Items.Add("26");
comboBox5.Items.Add("27");
comboBox5.Items.Add("28");
comboBox5.Items.Add("29");
comboBox5.Items.Add("30");
comboBox5.Items.Add("31");
}
private void comboBox6_Leave(object sender, EventArgs e)
{
//string s;
//s = (comboBox6.SelectedValue).ToString ();
//MessageBox.Show(s);
}
private void button1_Click(object sender, EventArgs e)
{
string mth;
double age;
year = Convert.ToInt32(comboBox7.Text);
mth = comboBox6.SelectedValue.ToString();
string a = comboBox7.Text + "." + mth;
double op = Convert.ToDouble(a);
age =2011.7-op ;
textBox1.Text = age .ToString ();
}
}
}