VB.NET – Monthly calendar interface
Steps I took to create a resizable monthly calendar that displays events such as birthdays. I show how i create the interface and create a class to provide info to populate, format and navigate the calendar.
Windows 10
.NET Framework 4.6.1
Windows Forms
‘Here is code that sets the days for a 6×7 calendar grid. It does not account for exceptions if trying to subtract from the minimum year and month
Private Sub SetDays()
Dim firstDayofMonth As DateTime
Dim column As Integer
Dim firstDayofGrid As DateTime
Dim gridDate As DateTime
firstDayofMonth = New Date(_Year, _Month, 1)
column = CInt(firstDayofMonth.DayOfWeek)
firstDayofGrid = firstDayofMonth.AddDays(column * -1)
gridDate = firstDayofGrid
For rowIndex = 0 To 5
For colIndex = 0 To 6
_Days(rowIndex, colIndex) = gridDate
gridDate = gridDate.AddDays(1)
Next
Next
Return
End Sub
‘Here is the code I used to populate all the days of the month
Private Sub PopulateCalendar()
Dim lbl As Control
Dim lblName As String
lbl = MonthYearContainer.Controls.Find(“LblMonthYear”, False).First
lbl.Text = String.Format(“{0} {1}”, MonthName(_CalendarInfo.Month), _CalendarInfo.Year)
For rowIndex = 0 To 5
For colIndex = 0 To 6
lblName = String.Format(“LblDayOfMonth{0}{1}”, rowIndex, colIndex)
lbl = Me.Controls.Find(lblName, True).First
lbl.Text = _CalendarInfo.DayInMonth(rowIndex, colIndex)
If _CalendarInfo.IsActiveMonth(rowIndex, colIndex) Then
lbl.ForeColor = Color.Black
Else
lbl.ForeColor = Color.Gray
End If
If _CalendarInfo.IsToday(rowIndex, colIndex) Then
lbl.ForeColor = Color.Red
End If
Next
Next
End Sub
Comments are closed.