We help IT Professionals succeed at work.

How to load a combo box with the next 30 years in windows forms?

ipjyo
ipjyo asked
on
I have a form to submit payment and I need to populate a combobox with the next 30 years for the user to choose their card's expiration year. Can anybody suggest how to implement this? Is this a good way or is it better to use calendar control?

Thank you!
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015
Commented:
It could be as simple as the following:

List<int> years = new List<int>(30);

for (int i = 0, current = DateTime.Now.Year; i < 30; i++, current++)
{
    years.Add(current);
}

this.comboBox1.DataSource = years;

Open in new window


Does it need to be more involved?

Author

Commented:
Thank you.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Glad it worked for you. I wasn't sure if you wanted something more elaborate.

Glad to help  = )