Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling a combobox with years

Tags:

c#

wpf

I am working on a WPF project and I have a years combobox that should contain years from 1950 to present year. Any ideas how proceed ?

like image 873
Adrian Avatar asked Aug 25 '11 09:08

Adrian


2 Answers

How about something like this, assign it as DataSource

Enumerable.Range(1950, DateTime.Today.Year).ToList();
like image 55
V4Vendetta Avatar answered Oct 11 '22 09:10

V4Vendetta


I would write a loop that starts at 1950 and ends at the current year. For every iteration of this loop just add an entry to the combobox with the current loop counter as content.

Some pseudocode:

for (int i = 1950; i <= currentYear; i++) {
   ComboBoxItem item = new ComboBoxItem();
   item.Content = i;
   myCombobox.Items.Add(item);
}
like image 25
RoflcoptrException Avatar answered Oct 11 '22 10:10

RoflcoptrException