Please i need help in leap year. My problem is, all of the month has a date of 1-31, except for feb. after i choose feb, as my month ( i'm using combo box ) , it removes the date from 29-31, and if i choose a leap year, it adds 29 to the day, my problem is when i select a different year, or month, it doesn't add the day 29-31.

here is my code.

Private Sub yearBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yearBox.SelectedIndexChanged
        If monthBox.SelectedIndex = 1 Then
            If IsNumeric(yearBox.Text) Then
                If Int(yearBox.Text) > 1950 And Int(yearBox.Text) < 1996 Then
                    If Date.IsLeapYear(Int(yearBox.Text)) Then
                        dayBox.Items.Add("29")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub monthBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles monthBox.SelectedIndexChanged
        If monthBox.SelectedIndex = 1 Then
            dayBox.Items.Remove("29")
            dayBox.Items.Remove("30")
            dayBox.Items.Remove("31")
        End If
    End Sub
End Class

Thank you in advance.

Recommended Answers

All 11 Replies

There is no code that you have given that adds days 30 or 31 (just 29 for leapyear). Why do you expect it to add days?

oh. i already did that, and didn't work also.

i've also tried this but it didn't work.

Private Sub yearBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yearBox.SelectedIndexChanged
        If monthBox.SelectedIndex = 1 Then
            If IsNumeric(yearBox.Text) Then
                If Int(yearBox.Text) > 1950 And Int(yearBox.Text) < 1996 Then
                    If Date.IsLeapYear(Int(yearBox.Text)) Then
                        dayBox.Items.Add("29")
                    Else
                        dayBox.Items.Remove("29")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub monthBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles monthBox.SelectedIndexChanged
        If monthBox.SelectedIndex = 1 Then
            dayBox.Items.Remove("29")
            dayBox.Items.Remove("30")
            dayBox.Items.Remove("31")
        Else
            dayBox.Items.Add("29")
            dayBox.Items.Add("30")
            dayBox.Items.Add("31")
        End If
    End Sub
End Class

Where do you add days 1-28 to the dayBox?

At the collection of the combobox.

I would do something like this instead of what you are doing. Might not be the greatest VB.Net code (I'm a C# programmer) but you'll get the idea :)

Private Sub yearBox_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yearBox.SelectedValueChanged
        If monthBox.SelectedIndex >= 0 Then
            set_dayBoxValues()
        End If
    End Sub

    Private Sub monthBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles monthBox.SelectedIndexChanged
        If yearBox.SelectedIndex >= 0 Then
            set_dayBoxValues()
        End If
    End Sub

    Private Sub set_dayBoxValues()
        Dim year As Integer = Int(yearBox.Text)
        Dim month As Integer = monthBox.SelectedIndex + 1
        Dim theDate As Date = New Date(year, month, 1)
        Dim count As Integer = 1
        dayBox.Items.Clear()
        Do Until theDate.Month <> month
            dayBox.Items.Add(count)
            count = count + 1
            theDate = theDate.AddDays(1)
        Loop
        dayBox.SelectedIndex = 0
    End Sub

just check leap year on monthbox event..so u didn't need to check on yearbox event.

1. Add the days until 31 on MonthBox Collection
2. Just check leap year on MonthBox Event, you didn't need to check on YearBox Event.

it's like this :

Private Sub monthBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthBox.SelectedIndexChanged
        Dim i As Integer
        If MonthBox.SelectedIndex = 1 Then
            If IsNumeric(YearBox.Text) Then
                If Int(YearBox.Text) > 1950 Or Int(YearBox.Text) < 1996 Then
                    If Date.IsLeapYear(Int(YearBox.Text)) Then
                        With DayBox.Items
                            For i = 1 To 29 ' if leap year
                                .Add(i)
                            Next
                        End With
                    Else
                        With DayBox.Items
                            For i = 1 To 28 ' if not leap year
                                .Add(i)
                            Next
                        End With
                    End If
                End If
            End If
        Else
            With DayBox.Items
                For i = 1 To 31
                    .Add(i)
                Next
            End With
        End If
    End Sub

or more shorter :

Private Sub AddDaysToMonthBox(ByVal Day As Integer)
        Dim i As Integer
        With DayBox.Items
            For i = 1 To Day
                .Add(i)
            Next
        End With
    End Sub

    Private Sub monthBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthBox.SelectedIndexChanged
        Dim i As Integer
        If MonthBox.SelectedIndex = 1 Then
            If IsNumeric(YearBox.Text) Then
                If Int(YearBox.Text) > 1950 Or Int(YearBox.Text) < 1996 Then
                    If Date.IsLeapYear(Int(YearBox.Text)) Then
                        AddDaysToMonthBox(29)
                    Else
                        AddDaysToMonthBox(28)
                    End If
                Else
                    AddDaysToMonthBox(31)
                End If
            End If
        End If
    End Sub

See if this helps.
yearBox has years as: 2010,2009,2008,etc.
monthBox has months by name as: January, February, March, etc.

Private Sub myCoolYearsAndMonths_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                         Handles yearBox.SelectedIndexChanged, monthBox.SelectedIndexChanged
        '// check if both boxes have values selected.
        If Not yearBox.SelectedIndex = -1 AndAlso Not monthBox.SelectedIndex = -1 Then
            dayBox.Items.Clear() '// clearDay box.
            '// loop from 1 to total days for that month during that year.
            For i As Integer = 1 To DateTime.DaysInMonth(CInt(yearBox.SelectedItem), monthBox.SelectedIndex + 1) '// +1 since index starts at 0, not 1
                dayBox.Items.Add(i) '// add day to dayBox.
            Next
        End If
        dayBox.SelectedIndex = dayBox.Items.Count - 1 '// view total days added.
    End Sub
commented: Yours is the simplest and understandable yet very useful, thank you sir. +1

Thank you for all the suggestions. thank you very much! Finally i'm done.

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.