HealthLinks is your destination for reliable, understandable, and credible health information and expert advice that always keeps why you came to us in mind.

Visual Basic .NET for Beginners

106 69
To practice some of the details that I mentioned on the previous page, we're going to use the MonthCalendar component to do something different: find the exact number of days between any two dates. This example is also going to be different because you should have learned enough to create the form in Visual Studio without a step-by-step description.

So, here's the finished form. You can create it in much the same way but with different components.

Remember to change the value of the "Checked" property for the first RadioButton component to "True".

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------


And here's the complete code for the project.

Public Class DaysBetweenDates    Dim FirstDateSave As Date    Dim SecondDateSave As Date    Private Sub MonthCalendar1_DateSelected( _       ByVal sender As Object, _       ByVal e As System.Windows.Forms.DateRangeEventArgs) _       Handles MonthCalendar1.DateSelected       If FirstDate.Checked Then          FirstDateSave = e.Start          FirstDateDisplay.Text = e.Start.ToShortDateString          SecondDate.Checked = True       Else          SecondDateSave = e.Start          SecondDateDisplay.Text = e.Start.ToShortDateString          FirstDate.Checked = True       End If    End Sub    Private Sub CalcDateDiff_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles CalcDateDiff.Click       DaysBetweenDatesDisplay.Text = _          CStr((SecondDateSave - FirstDateSave).TotalDays)    End Sub End Class

There are two "event subs" that make this program work. One is called when a date on the calendar is clicked, the other is called when the CalcDateDiff button component is clicked. The basic idea is that two dates are clicked in the calendar, then the second subroutine calculates the number of days between them. In the next lesson, we'll do a little debugging using this program and you'll learn a lot more about how and why it works.

One new thing that you will see in the first is the use of the "e" parameter. All VB.NET event subroutines will pass an "e" parameter, but they're different kinds of objects. The "Click" event for a button passes a "System.EventArgs" type. The "DateSelected" event passes the "System.Windows.Forms.DateRangeEventArgs" type. One property that you can use in the DateRangeEventArgs type is the "Start" property. (If you remember from Lesson 1, selected dates in the MonthCalendar component are always ranges. Again, just like before, the range is only one day so we could use the "End" property and this would still work.) Since this type is also an object, it has it's own methods and properties. One of them is "ToShortDateString".

Another new thing is the creation of a new "type" dynamically in the program code. Notice this line:

CStr((SecondDateSave - FirstDateSave).TotalDays)
The two "date" variables - SecondDateSave and FirstDateSave - when subtracted create an entirely new type called a TimeSpan type. This type has a method called TotalDays that makes the job easy. If you search the web, you can find whole articles written about more difficult ways to do it. It's a great example showing how working with .NET instead of against it can make your programming much faster and more productive.

This program could also have been designed using Button components instead of RadioButton components. Your assignment until the next lesson is to try writing it with Button components. What is the problem doing it that way?

Jump to Lesson 4 Now.
Source...

Leave A Reply

Your email address will not be published.