private void DrawListboxItem(object sender, DrawItemEventArgs e, ListBox lb) { MyListBoxItem item = lb.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem if (item != null) { e.Graphics.DrawString( // Draw the appropriate text in the ListBox item.Message, // The message linked to the item lb.Font, // Take the font from the listbox new SolidBrush(item.ItemColor), // Set the color 0, // X pixel coordinate e.Index * lb.ItemHeight // Y pixel coordinate. Multiply the index by the ItemHeight defined in the listbox. ); } }
private void DisplayAssignedShifts(List <Shift> dayShifts, DayOfWeek dayOfWeek, Time timeOfDay, ListBox lb) { Stack <MyListBoxItem> absentUsers = new Stack <MyListBoxItem>(); List <MyListBoxItem> lateUsers = new List <MyListBoxItem>(); List <MyListBoxItem> usersOnTime = new List <MyListBoxItem>(); Stack <MyListBoxItem> others = new Stack <MyListBoxItem>(); List <string> departments = new List <string>(); int currentHour = DateTime.Now.Hour; int currentMinutes = DateTime.Now.Minute; MyListBoxItem lateEmoji = new MyListBoxItem(Color.Yellow, "♦►"); MyListBoxItem absentEmoji = new MyListBoxItem(Color.Red, "!!"); MyListBoxItem onTimeEmoji = new MyListBoxItem(Color.Green, "☺"); foreach (Shift shift in dayShifts.Where(x => x.Time == timeOfDay)) { foreach (User user in shift.Users) { if (DateTime.Now.DayOfWeek > dayOfWeek || (DateTime.Now.DayOfWeek == dayOfWeek && DateTime.Now.Hour > shift.OriginalShiftStart.Hour)) { if (shift.EmployeeStart.Hour > shift.OriginalShiftStart.Hour) { MyListBoxItem late = new MyListBoxItem(Color.Orange, user.FirstName + " " + user.Dep.DptName); lateUsers.Add(late); } else if (shift.EmployeeStart.Hour <= shift.OriginalShiftStart.Hour && shift.EmployeeStart != DateTime.MinValue) { MyListBoxItem onTime = new MyListBoxItem(Color.Green, user.FirstName + " " + user.Dep.DptName); usersOnTime.Add(onTime); } else if (shift.EmployeeStart == DateTime.MinValue) { MyListBoxItem absent = new MyListBoxItem(Color.Red, user.FirstName + " " + user.Dep.DptName); absentUsers.Push(absent); } } else { //default black color MyListBoxItem other = new MyListBoxItem(Color.Black, user.FirstName + " " + user.Dep.DptName); others.Push(other); } departments.Add(user.Dep.DptName); } } List <string> allDepartments = dc.GetAllDepartments(); //Check if there is a department without an employee for the shift if (dayShifts.Where(x => x.Time == timeOfDay).ToList().Count < allDepartments.Count) { //get the departments without an employee and put them in the list of absent foreach (string dep in stController.GetAllDepartments()) { if (!departments.Contains(dep) && dep != "ALL" && dep != "NONE") { MyListBoxItem emptyDepartment = new MyListBoxItem(Color.Red, dep); absentUsers.Push(emptyDepartment); } } } //when there are more than 10items in the listbox - there is scroll and it bugs! //foreach (string d in departments) //{ // listBox1.Items.Add(d); //} //display first the list with absent, then with late and finally with onTime employees while (absentUsers.Count > 0) { lb.Items.Add(absentUsers.Pop()); } if (others.Count == 0) { foreach (MyListBoxItem item in lateUsers) { lb.Items.Add(item); } foreach (MyListBoxItem item in usersOnTime) { lb.Items.Add(item); } } else { foreach (MyListBoxItem item in others) { lb.Items.Add(item); } } }