I need this for 2 different changes.
ON one form i got a week calendar, the other a month calendar.

Was hoping to add 2 buttons previous & next.
To scroll back in time x days, or x days forward.
And similar for the months, but instead of days months.

Can anybody give me a exaple or a method how to get this to work?
Figured it could be done with a loop, but not sure anymore.

Thanks

Dani AI

Generated

A simple pattern works for both views: keep one "anchor" DateTime that represents what the UI is showing (the week-start for a week view, or the visible month for a month view). Change that anchor by a fixed amount on Prev/Next (7 days for a week, 1 month for a month), persist the anchor across requests or state (ViewState/session/query string for web; a field/property for desktop) and re-render the calendar. demonstrated the same idea for a desktop MonthCalendar — no loops required.

To make week navigation robust, compute the start of the week from any date (respecting the culture's first day of week) and use that as the anchor. Example helper (VB.NET):

Function StartOfWeek(d As Date, firstDay As DayOfWeek) As Date
    Dim diff As Integer = (7 + (CInt(d.DayOfWeek) - CInt(firstDay))) Mod 7
    Return d.AddDays(-diff).Date
End Function

You can get the culture default first day via CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek (DateTimeFormatInfo.FirstDayOfWeek). Use the returned start as your anchor, then AddDays(+/-7) for week buttons.

Practical notes by platform: in ASP.NET WebForms store the anchor in ViewState or in the query string and set the calendar control's VisibleDate to that anchor so the month changes without altering selection (Calendar.VisibleDate). In desktop apps use the MonthCalendar/DateTimePicker controls and update their visible/selected date programmatically (MonthCalendar docs).

Edge cases and tips: test month-boundaries and Feb 28/29 (DateTime.AddMonths adjusts to the last valid day — see DateTime.AddMonths), enforce min/max dates, and decide whether Prev/Next should preserve day-of-month or always jump to first-of-month. If the app is browser-based and needs richer UI, consider a client-side calendar and pass the anchor back to the server so links remain bookmarkable.

Recommended Answers

All 2 Replies

For using MonthCalendar control and when you want to go 1 month forward and backward when pressing buttons:

Private Sub button1_Click(sender As Object, e As EventArgs)
    '1 month forward
    Dim [date] As DateTime = monthCalendar1.SelectionStart
    monthCalendar1.SelectionStart = [date].AddMonths(1)
    monthCalendar1.SelectionEnd = monthCalendar1.SelectionStart
End Sub

Private Sub button2_Click(sender As Object, e As EventArgs)
    '1 month backward
    Dim [date] As DateTime = monthCalendar1.SelectionStart
    monthCalendar1.SelectionStart = [date].AddMonths(-1)
    monthCalendar1.SelectionEnd = monthCalendar1.SelectionStart
End Sub

In case if you want use to select only one day, dont forget to set property "monthCalendar1.MaxSelectionCount" to 1 on load time.

Stupid me, completely forgot about the datetimepicker, can make it a lot easier this way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.