Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to programming and trying to figure out how to set up a auto populated drop down list for year.

I am trying to set up a drop list of years, starting from 2009 to current year. I have tried to use the code below but it failed.

Can someone please help me to look into the coding and explain to me what I did wrong? Any help is greatly appreciated!

I am using Visual Studio 2005 to programe.

VB
Private Sub cbo_StartYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbo_StartYear.SelectedIndexChanged

        cbo_StartYear.Items.Clear()  'CLEAR THE LIST FIRST
        Dim currentYear As Integer = Date.Now.Year
        Dim i As Integer
        For i = 2009 To currentYear
            cbo_StartYear.Items.Add(i)
        Next i
        cbo_StartYear.SelectedIndex = cbo_StartYear.Items.Count - 1
End Sub
Posted
Updated 16-Jan-12 16:35pm
v3
Comments
Om Prakash Pant 16-Jan-12 22:19pm    
put the code in form load event instead of cbo_StartYear_SelectedIndexChanged

1 solution

As Om Prakash said, you should put this code in Form_Load or Control_Load event.
Just like,
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cbo_StartYear.Items.Clear()  'CLEAR THE LIST FIRST
        Dim currentYear As Integer = Date.Now.Year
        Dim i As Integer
        For i = 2009 To currentYear
            cbo_StartYear.Items.Add(i)
        Next i
        cbo_StartYear.SelectedIndex = cbo_StartYear.Items.Count - 1
End Sub
 
Share this answer
 
Comments
Boon How 17-Jan-12 0:50am    
Ya the coding works fine now :)
Thanks you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900