Yuriy Faktorowitsch des Code oben als Basis, hier ist, wie eine Liste der Termine für eine bestimmte Anzahl von Wochen in LongDateString Format zu bekommen, und sie zu einem Kombinationsfeld zuweisen. Dies nutzt „Montag“, aber Sie können einfach ersetzen „Montag“ mit jedem anderen DOW Ihre Zwecke anpassen:
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;
}
... und, wenn Sie das aktuelle Datum an das Kombinationsfeld hinzufügen möchten auch Sie kann ein Wörterbuch wie folgt verwenden:
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;
}
Was sehen Sie? Haben Sie die Eigenschaft DisplayMember zugewiesen? –