I'm fairly new to coding in VB and after much experimenting I can't find a way to do this. I'm trying to have a combo box display a range of years starting at one specified in a date variable and running to the current date, with the current date one being displayed as the default. Here is my latest version of the code trying to set up the datasource.
Dim yearDataSource = Enumerable.
Range(myEarliestDate.Year, myEarliestDate.Year - DateTime.Now.Year 1).
OrderByDescending(Function(y) y).
ToList()
I'd appreciate any help you could give. Please keep it simple.
CodePudding user response:
This function will work
Private Function GetYears(startDate As Date, endDate As Date) As List(Of Integer)
Dim startYear = startDate.Year
Dim endYear = endDate.Year
Dim years = (From year In Enumerable.Range(startYear, endYear - startYear)
Order By year Descending)
Return years.ToList()
End Function
Example call:
Dim startDate = Date.Now.AddYears(-10)
Dim endDate = Date.Now.AddYears(1)
cmbYears.DataSource = GetYears(startDate, endDate)