0

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.

4
Contributors
11
Replies
13
Views
6 Years
Discussion Span
Last Post by Jake.20
Featured Replies
  • See if this helps. yearBox has years as: 2010,2009,2008,etc. monthBox has months by name as: January, February, March, etc. [CODE] 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 … Read More

0

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?

0

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
0

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
0

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

Edited by Jx_Man: n/a

0

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

Edited by Jx_Man: n/a

0

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

Edited by Jx_Man: n/a

1

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
Votes + Comments
Yours is the simplest and understandable yet very useful, thank you sir.
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.