kbronciyo
Joke Master
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
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”.
Double click the Form and do the following code above the Form_Load
for declaring the variables.
Then, go back the Design Views, double click the Textbox(txtafter) and do the following code.
After that, go back to the Design Views again, double click the Textbox(txtbefore) and do the following code.
You can download the complete source.
Download
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”.

Double click the Form and do the following code above the Form_Load
for declaring the variables.
- 'DECLARING THE VARIABLES
- Dim
intervaldate As
Integer
'REPRESENTS A DATE INTERVAL
- Dim
dateresult As
Date
'REPRESENTS THE ADDED DATE OF START DATE AND THE INTERVAL DAYS
- 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.
- Private
Sub
txtafter_TextChanged(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
txtafter.
TextChanged
- Try
- 'CONDITIONING THE AFTER DAYS INTERVAL
- If
txtafter.
Text
.
Length
>
0
Then
'CHECK THE INTERVAL DAYS IF GREATER THAN 0.
- intervaldate =
CInt
(
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
- End
If
- Catch
ex As
Exception
- MsgBox
(
ex.
Message
)
- End
Try
- End
Sub
After that, go back to the Design Views again, double click the Textbox(txtbefore) and do the following code.
- Private
Sub
txtbefore_TextChanged(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
txtbefore.
TextChanged
- Try
- 'CONDITIONING THE BEFORE DAYS INTERVAL
- If
txtbefore.
Text
.
Length
>
0
Then
- intervaldate =
CInt
(
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
(
)
- End
If
- Catch
ex As
Exception
- MsgBox
(
ex.
Message
)
- End
Try
- End
Sub
You can download the complete source.
Download
You must upgrade your account or reply in the thread to view the hidden content.