What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSet the MonthCalendar control's FirstDay property correctly for this computer in VB.NET
DescriptionThis 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.
KeywordsWeekDayName, internationalization, first day of the week, locale, VB.NET
CategoriesSoftware 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 = _
        Weekday(first_date, FirstDayOfWeek.System)

    ' Convert into a Day.
    Dim first_day As Day
    Select Case first_day_number
        Case FirstDayOfWeek.Sunday
            first_day = Day.Sunday
        Case FirstDayOfWeek.Monday
            first_day = Day.Monday
        Case FirstDayOfWeek.Tuesday
            first_day = Day.Tuesday
        Case FirstDayOfWeek.Wednesday
            first_day = Day.Wednesday
        Case FirstDayOfWeek.Thursday
            first_day = Day.Thursday
        Case FirstDayOfWeek.Friday
            first_day = Day.Friday
        Case FirstDayOfWeek.Saturday
            first_day = Day.Saturday
    End Select

    ' Set the calendar's first day of the week number.
    MonthCalendar1.FirstDayOfWeek = first_day
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.