/* * take start date * see if the start date belongs * if it belongs add to the list * increment start date by 1 day * * how to know if it belongs: * getweekday -> compare it to all values of recurring array * */ public void CalcRecurringDates() { //Console.WriteLine("loop1"); DateTime tempDate = new DateTime(dateStart.Year, dateStart.Month, dateStart.Day, dateStart.Hour, dateStart.Minute, dateStart.Second); bool foundAll = false; // || recurringType[0] != "Weekly")) ; if (!recurring && (Object.Equals(recurringType, default(string[])))) { while (!foundAll) { recurringDates.Add(tempDate); tempDate = DateUtil.AddDay(tempDate); if (DateUtil.DatesEqual(tempDate.AddDays(-1), dateEnd)) { foundAll = true; } } } while (!foundAll) // what if start and end dates are reversed or equal? //what if selected weekly and daily and mondays for ex? { if (recurringType.Contains("Weekly")) { recurringDates.Add(tempDate); tempDate = DateUtil.AddDay(tempDate, 7); //Console.WriteLine("loop2"); } else if (((recurringType.Length > 1) && recurringType[1].ToString().Contains("Daily")) || !recurring) { recurringDates.Add(tempDate); tempDate = DateUtil.AddDay(tempDate); //Console.WriteLine("loop3"); } //else if (Array.Exists(recurringType, element => element == tempDate.DayOfWeek.ToString())) else if (recurringType.Contains(tempDate.DayOfWeek.ToString())) { DateTime recurringDay = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day); recurringDates.Add(recurringDay); tempDate = DateUtil.AddDay(tempDate); if (DateUtil.DatesEqual(tempDate.AddDays(-1), dateEnd)) { foundAll = true; break; } } else { tempDate = DateUtil.AddDay(tempDate); } if (tempDate >= dateEnd) { foundAll = true; break; } } }
// Nothing like initialising calendar squares in two tripple-nested loops! public void InitCalSquares() { int daysInMonth = DateUtil.DaysInMonth(CurrentMonth.Year, CurrentMonth.Month); int firstWeekDay = DateUtil.FirstWeekDay(CurrentMonth.Year, CurrentMonth.Month); squares = new CalSquare[daysInMonth]; emptySquares = new CalSquare[7]; int dayIndex = 1; ArrayList Events = XmlControl.GetEventsList(); DateTime tempDate = CurrentMonth; //DateTime LastMonthDay = new DateTime(CurrentMonth.Year, CurrentMonth.Month, // DateTime.DaysInMonth(CurrentMonth.Year, CurrentMonth.Month)); // for (int i = 0; i < daysInMonth; i++) { squares[i] = new CalSquare(dayIndex); squares[i].SetDate(new DateTime(CurrentMonth.Year, CurrentMonth.Month, i + 1)); squares[i].GetSquare().Click += new EventHandler(SquareHandler); squares[i].GetLabel1().Click += new EventHandler(SquareHandler); squares[i].GetLabel2().Click += new EventHandler(SquareHandler); squares[i].GetDayLabel().Click += new EventHandler(SquareHandler); dayIndex++; //foreach (CalEvent e in Events) // refactor to for loops, they re faster for (int j = 0; j < Events.Count; j++) { // gets dates which are before current date ArrayList dates = ((CalEvent)Events[j]).GetDates(new DateTime(CurrentMonth.Year, CurrentMonth.Month, i + 1)); //foreach (var d in dates) for (int x = 0; x < dates.Count; x++) { if (DateUtil.DatesEqual((DateTime)dates[x], (new DateTime(CurrentMonth.Year, CurrentMonth.Month, i + 1)))) { squares[i].AddEvent(((CalEvent)Events[j]).GetTitleAndStartTimeString()); } } } } Boolean noLabel = true; for (int i = 0; i < 6; i++) { emptySquares[i] = new CalSquare(noLabel); } int index = 0; bool firstCycle = true; int tempIndex; for (int i = 3; i < 10; i++) // rows { for (int j = 1; j <= 7; j++) // columns { if (index >= daysInMonth) { return; // stop all the nonsense if run out of days } if (firstCycle) // if first cycle, add empty squares { tempIndex = index; // holding this as its needed for real squares while (j < firstWeekDay) // adding dummy squares for the first row { frame.Controls.Add(emptySquares[index].GetSquare(), j, i); j = j + 1; index++; } firstCycle = false; index = tempIndex; // reseting index for real squares } if (j == 7) // for sundays set color { squares[index].GetSquare().BackColor = Color.FromArgb(255, 255, 243, 230); } else { squares[index].GetSquare().BackColor = Color.FromArgb(255, 227, 223, 213); } frame.Controls.Add(squares[index].GetSquare(), j, i); index++; } } //NoDaysLeft:; }