En utilisant le code de Yuriy Faktorovich ci-dessus comme base, voici comment obtenir une liste des dates au format LongDateString pour un nombre donné de semaines, et les affecter à une zone de liste déroulante. Celui-ci utilise « lundi », mais vous pouvez simplement remplacer « lundi » avec tout autre DOW pour répondre à vos besoins:
private void PopulateSchedulableWeeks()
{
int WEEKS_COUNT = 13;
List<String> schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList();
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
}
public static List<String> GetWeekBeginnings(int countOfWeeks)
{
// from http://stackoverflow.com/questions/6346119/datetime-get-next-tuesday
DateTime today = DateTime.Today;
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
DateTime nextMonday = today.AddDays(daysUntilMonday);
List<String> mondays = new List<string>();
mondays.Add(nextMonday.ToLongDateString());
for (int i = 0; i < countOfWeeks; i++)
{
nextMonday = nextMonday.AddDays(7);
mondays.Add(nextMonday.ToLongDateString());
}
return mondays;
}
... et, si vous voulez ajouter la date réelle à la zone de liste déroulante, aussi, vous peut utiliser un dictionnaire comme ça:
int WEEKS_TO_OFFER_COUNT = 13;
BindingSource bs = new BindingSource();
Dictionary<String, DateTime> schedulableWeeks = AYttFMConstsAndUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT); bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
public static Dictionary<String, DateTime> GetWeekBeginningsDict(int countOfWeeks)
{
DateTime today = DateTime.Today;
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
DateTime nextMonday = today.AddDays(daysUntilMonday);
Dictionary<String, DateTime> mondays = new Dictionary<String, DateTime>();
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
for (int i = 0; i < countOfWeeks; i++)
{
nextMonday = nextMonday.AddDays(7);
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
}
return mondays;
}
Que voyez-vous? Avez-vous attribué la propriété DisplayMember? –