Reputation: 10247
I need to let the user select a month and a year. Usually, they will want the last month (e.g., if it's December 18th, 2015, they will normally want to select November 2015). So I want those values to be the ones pre-selected in the comboboxes.
How can I do this (in C#) and also have it be "December 2015" once crossing the Rubicon (Euphrates?) to January 2016?
I also want it to not add 2016 until it is 2016 (and so on, as the years go by) and start from a predetermined year (not year 0 or 1970 or anything like that, necessarily).
Upvotes: 1
Views: 8280
Reputation: 125197
You can use DateTimeFormatInfo.MonthNames
to find month names for a culture.
Some cultures have 13 months, so DateTimeFormat.MonthNames
returns an array with 13 month.
But since it seems you want to use Gregorian calendar, you only need to use 12 month names and I used InvariantCulture
to get month names.
You can use such code:
monthComboBox.DataSource = CultureInfo.InvariantCulture.DateTimeFormat
.MonthNames.Take(12).ToList();
monthComboBox.SelectedItem = CultureInfo.InvariantCulture.DateTimeFormat
.MonthNames[DateTime.Now.AddMonths(-1).Month - 1];
yearComboBox.DataSource = Enumerable.Range(1983, DateTime.Now.Year - 1983 + 1).ToList();
yearComboBox.SelectedItem = DateTime.Now.Year;
You can use CurrentCulture
or some other culture you prefer.
Upvotes: 7
Reputation: 10247
This should work (assuming the form on which your comboboxes live is named "mainForm", and the comboboxes are named "comboBoxMonth" and "comboBoxYear"):
private void mainForm_Shown(object sender, EventArgs e)
{
PopulateMonthsAndYears();
}
private void PopulateMonthsAndYears()
{
const int DECEMBER = 11;
const int BEGIN_YEAR = 1983; // KCMS
comboBoxMonth.Items.AddRange(PlatypusConstsAndUtils.MonthsFull.ToArray<object>());
comboBoxMonth.SelectedIndex = PlatypusConstsAndUtils.GetIndexForPreviousMonth();
comboBoxYear.DataSource = Enumerable.Range(BEGIN_YEAR, DateTime.Now.Year - BEGIN_YEAR + 1).ToList();
comboBoxYear.SelectedIndex = comboBoxYear.Items.IndexOf(DateTime.Now.Year);
// However, if it is January (and thus the month is set to December), set the year to previous also
if (comboBoxMonth.SelectedIndex == DECEMBER)
{
comboBoxYear.SelectedIndex = comboBoxYear.Items.IndexOf(DateTime.Now.Year - 1);
}
}
...and the class that holds the month vals (of course, you can change the string vals to Spanish or German or Klingon or whatever "floats your boat"):
public static class PlatypusConstsAndUtils
{
public static List<string> MonthsFull = new List<string>
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
public static int GetIndexForPreviousMonth()
{
// Months are 1-based, but return the index, which is 0-based, so decrement it
const int JANUARY = 1;
const int DECEMBER = 12;
int prevMonth;
int currentMonth = DateTime.Now.Month;
if (currentMonth == JANUARY)
{
prevMonth = DECEMBER - 1;
}
else
{
prevMonth = DateTime.Now.Month - 2;
}
return prevMonth;
}
. . .
Upvotes: 1