Date Calculator in Visual Basic 2008

Today I’m going to show you how to create a simple Date Calculator in Visual Basic 2008.With 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 lets begin: Open Visual Basic 2008, create a new Windows Application and do the Form just like this. Name the two textboxes, the first is “txtafter” and the other one is “txtbefore” and the DateTimePicker is “dtpstrdate”. first form Double click the Form and do the following code above the Form_Load for declaring the variables.
  1. 'DECLARING THE VARIABLES
  2.     Dim intervaldate As Integer 'REPRESENTS A DATE INTERVAL
  3.     Dim dateresult As Date 'REPRESENTS THE ADDED DATE OF START DATE AND THE INTERVAL DAYS
  4.     Dim msg As String 'REPESENTS A MESSAGE TO PUT INTO A LISTBOX
Then, go back the Design Views, double click the Textbox(txtafter) and do the following code.
  1.    Private Sub txtafter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtafter.TextChanged
  2.         Try
  3.             'CONDITIONING THE AFTER DAYS INTERVAL
  4.             If txtafter.Text.Length > 0 Then 'CHECK THE INTERVAL DAYS IF GREATER THAN 0.
  5.                 intervaldate = CInt(txtafter.Text) 'ASSIGNING A TEXTBOX TO A VARIALBLE
  6.                 'FORMULA
  7.                 dateresult = dtpstrdate.Value.AddDays(intervaldate).ToString() 'ADDING THE INTERVAL DAYS TO THE START DATE.
  8.                 msg = "After " & intervaldate & " day(s), the date is " 'MESSAGE TO BE PUT IN THE LISTBOX
  9.                 ListBox1.Items.Clear() 'CLEARING A LISTBOX
  10.                 ListBox1.Items.Add(msg & Format(dateresult, "dddd, MMMM dd, yyyy")) 'ADDING A MESSAGE AND FORMATTED DATE RESULT IN THE LISTBOX
  11.                 txtbefore.Clear() 'CLEARING THE TEXTBOX
  12.             Else
  13.                 ListBox1.Items.Clear() 'CLEARING A LISTBOX
  14.             End If
  15.         Catch ex As Exception
  16.             MsgBox(ex.Message)
  17.         End Try
  18.  
  19.  
  20.     End Sub
After that, go back to the Design Views again, double click the Textbox(txtbefore) and do the following code.
  1.     Private Sub txtbefore_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbefore.TextChanged
  2.         Try
  3.             'CONDITIONING THE BEFORE DAYS INTERVAL
  4.             If txtbefore.Text.Length > 0 Then
  5.                 intervaldate = CInt(txtbefore.Text)
  6.                 'FORMULA
  7.                 'SYNTAX...
  8.                 'THE FORMULA IS THE SAME BUT THE TWIST IS
  9.                 'I ADD A NEGATIVE INTERVAL SO THAT IT WILL GO BACK TO THE PREVIOUS DATE.
  10.                 dateresult = dtpstrdate.Value.AddDays(-intervaldate).ToString()
  11.                 msg = "Before " & intervaldate & " day(s), the date is "
  12.                 ListBox1.Items.Clear()
  13.                 ListBox1.Items.Add(msg & Format(dateresult, "dddd, MMMM dd, yyyy"))
  14.                 txtafter.Clear()
  15.             Else
  16.                 ListBox1.Items.Clear()
  17.             End If
  18.         Catch ex As Exception
  19.             MsgBox(ex.Message)
  20.         End Try
  21.     End Sub
You can download the complete source.

Add new comment