// 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:; }