Filling the month and day

I populate the combobox with the name of the month as follows: `

var engCulture = new CultureInfo("en-US"); cmbAmCul.Items.AddRange(engCulture.DateTimeFormat.MonthNames); 

What is the correct way:

  • exclude blank entry after 12 months.
  • fill out another combobox with a day (1-31 || 1-28 || 1-30) depending on the month selected?
0
source share
3 answers

In the first part, you can use the linq query ...

 var engCulture = new CultureInfo("en-US"); cmbAmCul.Items.AddRange(from m in engCulture.DateTimeFormat.MonthNames where !String.IsNullOrEmpty(m) select m); 

The DateTimeInfo class supports calendars with 13 months, so this is happening.

For the second part, I would go for something like ...

 for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++) { cmbDay.Items.Add(i.ToString()); } 

Obviously, the population of the shafts is year / month from your selected values.

+8
source
  • Use String.Trim () to remove spaces from the beginning and end of lines.
  • Use the return value from DateTime.DaysInMonth(int year, int month) to populate the list of days.

You can also use the DateTime selector from standard controls or is this not an option for you?

+3
source

You do not indicate what you are trying to build (ASP.NET or Winforms, etc.), but you can use the Calendar control instead of drop-down lists to improve the user interface. Less clicks and less code if you're wrong.

0
source

All Articles