This is a simple tutorial in which we will create a simple Date Calculator program made in C#. On this, it will help you know the dates before and after the present date. For instance, you want to jump into seven days after the present date, the date calculator will automatically calculate it. And you don’t have to count the days anymore, so the user will benefit this kind of program.
So, now let's start this tutorial!
1. Let's start with creating a
Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to
File, click
New Project, and choose
Windows Application.
2. Next, add two textboxes, the first is “
txtafter” and the other one is
“txtbefore” and the DateTimePicker is “
dtpstrdate” and one listbox named ListBox1.You must design your interface like this:
3. Put the following code below in your Form_Load.
//declaring the variables
int intervaldate; //represents a date interval
DateTime dateresult; //represents the added date of start date and the interval days
string msg; //repesents a message to put into a listbox
4. Have the code below for txtafter_TextChanged, put this code below.
private void txtafter_TextChanged(System.Object sender, System.EventArgs e)
{
try
{
//conditioning the after days interval
if (txtafter.Text.Length > 0) //check the interval days if greater than 0.
{
intervaldate = system.convert.toint32(txtafter.text); //assigning a textbox to a varialble
//formula
dateresult = dtpstrdate.Value.AddDays(intervaldate).ToString(); //adding the interval days to the start date.
msg = "After " + intervaldate + " day(s), the date is "; //message to be put in the listbox
ListBox1.Items.Clear(); //clearing a listbox
ListBox1.Items.Add(msg + Format(dateresult, "dddd, MMMM dd, yyyy")); //adding a message and formatted date result in the listbox
txtbefore.Clear(); //clearing the textbox
}
else
{
ListBox1.Items.Clear(); //clearing a listbox
}
}
catch (Exception ex)
{
MessageBox.Show((string) ex.Message);
}
}
5. Have the code below for txtbefore_TextChanged, put this code below.
private void txtbefore_TextChanged(System.Object sender, System.EventArgs e)
{
try
{
//conditioning the before days interval
if (txtbefore.Text.Length > 0)
{
intervaldate = System.Convert.ToInt32(txtbefore.Text);
//formula
//syntax...
//the formula is the same but the twist is
//i add a negative interval so that it will go back to the previous date.
dateresult = dtpstrdate.Value.AddDays(- intervaldate).ToString();
msg = "Before " + intervaldate + " day(s), the date is ";
ListBox1.Items.Clear();
ListBox1.Items.Add(msg + Format(dateresult, "dddd, MMMM dd, yyyy"));
txtafter.Clear();
}
else
{
ListBox1.Items.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show((string) ex.Message);
}
}
Output:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware