|Title||Set the MonthCalendar control's FirstDay property correctly for this computer in VB.NET|
|Description||This example shows how to set the MonthCalendar control's FirstDay property correctly for this computer in VB.NET. The program subtracts the current date's weekday number from the current day to get the first date of the week.|
|Keywords||WeekDayName, internationalization, first day of the week, locale, VB.NET|
|Categories||Software Engineering, VB.NET|
The program uses DateAdd to subtract the current date's day number in days from the current date. This gives the date that is first in the week. It passes Weekday the parameter FirstDayOfWeek.System so it numbers the date's weekday number using the system's locale information.
The program then uses Weekday to get the date's day number, again using the parameter FirstDayOfWeek.System so it uses the system's locale.
Next the program needs to convert the first day number into the Day enumeration. Unfortunately day numbers don't use the same numbering system as Day so the program uses a Select Case statement to make the convesion.
Finally the program sets the MonthCalendar control's FirstDayOfWeek property to the correct Day value.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Get the system's first day of the week.
Dim first_date As Date = _
DateAdd("d", -(Weekday(Now, FirstDayOfWeek.System) _
- 1), Now)
' Convert into a day number.
Dim first_day_number As Integer = _
' Convert into a Day.
Dim first_day As Day
Select Case first_day_number
first_day = Day.Sunday
first_day = Day.Monday
first_day = Day.Tuesday
first_day = Day.Wednesday
first_day = Day.Thursday
first_day = Day.Friday
first_day = Day.Saturday
' Set the calendar's first day of the week number.
MonthCalendar1.FirstDayOfWeek = first_day