Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Date Question

Posted on 2005-03-09
Medium Priority
Last Modified: 2010-04-23
If I had a simple form with one textbox and a listbox and I wanted to type the year into the textbox (ex. 2004 or 2005 etc) and have the listbox populate with
all the dates "??/??/????" that start each week.  Like all the Sundays.  How would that be done?

Any Suggestions?

Question by:swilli6926
  • 2
LVL 12

Expert Comment

ID: 13500221
Ok, this may need more testing, but this seems to work (areas that may need to be tested is when the 31/12/xxxx is a sunday)

Sub ListAllSundays(ByVal YearNum As Integer, Byval ListX as Listbox)
        Dim i As Integer
        Dim thisDate As Date
        Dim firstSunday As Integer
        For i = 1 To 7
            thisDate = New Date(YearNum , 1, i)
            If thisDate.DayOfWeek = DayOfWeek.Sunday Then
                Exit For
            End If

        Do While thisDate.Year = YearNum
            thisDate = thisDate.AddDays(7)
End Sub
LVL 86

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 13500236
Something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim year As Integer = Integer.Parse(TextBox1.Text)
            DayOfWeekForEntireYear(year, vbSunday)
        Catch ex As Exception
            MessageBox.Show(TextBox1.Text, "Invalid Year", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub DayOfWeekForEntireYear(ByVal year As Integer, ByVal DayOfWeek As Integer)
        If DayOfWeek < 1 Or DayOfWeek > 7 Then
            MessageBox.Show("DayOfWeek must be between 1 (Sunday) and 7 (Saturday)", "Invalid DayOfWeek", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

            Dim dt As DateTime = New DateTime(year, 1, 1)
            While DatePart(DateInterval.Weekday, dt) <> DayOfWeek
                dt = dt.AddDays(1)
            End While

            While dt.Year = year
                dt = dt.AddDays(7)
            End While
        Catch ex As Exception
            MessageBox.Show(TextBox1.Text, "Invalid Year", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
LVL 12

Expert Comment

ID: 13500250
Ok... Idle's is a lot neater than mine (and takes into account different days :P)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question