Adrian
Adrian

Reputation: 20068

Filling a combobox with years

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 ?

Upvotes: 6

Views: 20102

Answers (5)

bbedson
bbedson

Reputation: 143

Start with current year by adding Reverse. Also Enumerable Range requires using System.Linq.

Enumerable.Range(1950, DateTime.UtcNow.Year - 1949).Reverse().ToList();

Upvotes: 2

Shabek Mahmood
Shabek Mahmood

Reputation: 11

        for (int i = 1950; i <= 2050; i++)
        {              

           Year_combo.Items.Add(i);
        }

Upvotes: 0

RoflcoptrException
RoflcoptrException

Reputation: 52229

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);
}

Upvotes: 9

V4Vendetta
V4Vendetta

Reputation: 38210

How about something like this, assign it as DataSource

Enumerable.Range(1950, DateTime.Today.Year).ToList();

Upvotes: 13

Davide Piras
Davide Piras

Reputation: 44605

something like:

for(int year = 1950; year<DateTime.UtcNow.Year; ++year)
{
// Add year as year to the combo box item source...
}

Upvotes: 2

Related Questions