Help me to fix BUG with Appointments layout

Jan 25, 2011 at 2:19 PM

Try to add this Appointments:

#region " Add Some Appointments "
m_Appointments = new List<Appointment>();

DateTime m_Date = DateTime.Now;

m_Date = m_Date.AddHours(19 - m_Date.Hour);
m_Date = m_Date.AddMinutes(-m_Date.Minute);

Appointment m_Appointment = new Appointment();

m_Appointment.StartDate = m_Date;
m_Appointment.EndDate = m_Date.AddHours(1);
m_Appointment.Subject = "19:00";

m_Appointments.Add(m_Appointment);


m_Appointment = new Appointment();

m_Appointment.StartDate = m_Date;
m_Appointment.EndDate = m_Date.AddHours(1);
m_Appointment.Subject = "19:00 (2)";
m_Appointments.Add(m_Appointment);

m_Date = m_Date.AddMinutes(40);

m_Appointment = new Appointment();
m_Appointment.StartDate = m_Date;
m_Appointment.EndDate = m_Date.AddHours(1);
m_Appointment.Subject = "19:40";
m_Appointment.Color = System.Drawing.Color.LightBlue;


m_Appointments.Add(m_Appointment);

m_Date = m_Date.AddMinutes(20);

m_Appointment = new Appointment();
m_Appointment.StartDate = m_Date;
m_Appointment.EndDate = m_Date.AddHours(1);
m_Appointment.Color = System.Drawing.Color.Yellow;
m_Appointment.Subject = "20:00";

m_Appointments.Add(m_Appointment);

m_Date = m_Date.AddMinutes(10);

m_Appointment = new Appointment();
m_Appointment.StartDate = m_Date;
m_Appointment.EndDate = m_Date.AddHours(1);
m_Appointment.Subject = "20:10";
//m_Appointment.AllDayEvent = true;
m_Appointment.Color = System.Drawing.Color.Red;

m_Appointments.Add(m_Appointment);

#endregion

Appointments "20:00" and "20:10" stratify.

Thank you for any help.

 

 

Jan 30, 2011 at 4:57 PM

I solve my problem!

I changed some functions.

1) GetAppointmentAt
(I get all appointments at cursor and select them in turn if right mouse button was clicked)


public Appointment GetAppointmentAt(int x, int y)
{
if (y < this.HeaderHeight)
return null;

foreach (AppointmentView view in appointmentViews.Values)
if (view.Rectangle.Contains(x, y))
return view.Appointment;

return null;
}
replaced by:

public Appointment GetAppointmentAt(int x, int y, MouseEventArgs e)
{
if (SelectedAppointmentAt(e.X, e.Y) && e.Button == MouseButtons.Left)
return selectedAppointment;

List<Appointment> Appointments = GetAllAppointmentsAt(x, y);
if (EqualsAppointments(AppointmentsAtCursor, Appointments) && e.Button == MouseButtons.Right) //
{
if (AppointmentIndex + 1 >= Appointments.Count)
AppointmentIndex = 0;
else
AppointmentIndex++;
return Appointments[AppointmentIndex];
}
else
{
if (Appointments.Count > 0)
{
AppointmentsAtCursor = Appointments;
return Appointments[0];
}
else
return null;
}
}



Also added:

private List<Appointment> AppointmentsAtCursor = null;
private int AppointmentIndex = 0;


private bool EqualsAppointments(List<Appointment> list1, List<Appointment> list2)
{
if (list1 == null || list2 == null)
return false;
if (list1.Count != list2.Count)
return false;
for (int index=0; index < list1.Count; index ++)
{
if (list1[index] != list2[index])
return false;
}
return true;
}


public List<Appointment> GetAllAppointmentsAt(int x, int y)
{
List<Appointment> Appointments = new List<Appointment>();
if (y < this.HeaderHeight)
return null;

foreach (AppointmentView view in appointmentViews.Values)
if (view.Rectangle.Contains(x, y))
Appointments.Add(view.Appointment);

return Appointments;
}

public bool SelectedAppointmentAt(int x, int y)
{
if (selectedAppointment == null)
return false;
List<Appointment> Appointments = GetAllAppointmentsAt(x, y);
if(Appointments.Contains(selectedAppointment))
return true;
else
return false;
}



Also I changed order of drawing:

private void DrawDays(PaintEventArgs e, Rectangle rect)
{
int dayWidth = rect.Width / daysToShow;

DateTime time = startDate;
Rectangle rectangle = rect;
rectangle.Width = dayWidth;

appointmentViews.Clear();

for (int day = 0; day < daysToShow; day++)
{
DrawDay(e, rectangle, time);

rectangle.X += dayWidth;
time = time.AddDays(1);
}
}

replaced by:

private Rectangle selectedRectangle;

private void DrawDays(PaintEventArgs e, Rectangle rect)
{
int dayWidth = rect.Width / daysToShow;
DateTime time = startDate;
Rectangle rectangle = rect;
rectangle.Width = dayWidth;
appointmentViews.Clear();

//DrawDays
for (int day = 0; day < daysToShow; day++)
{
DrawDay(e, rectangle, time);

rectangle.X += dayWidth;
time = time.AddDays(1);
}

time = startDate;
rectangle = rect;
rectangle.Width = dayWidth;

//DrawAppointments
for (int day = 0; day < daysToShow; day++)
{
DrawAppointments(e, rectangle, time);
rectangle.X += dayWidth;
time = time.AddDays(1);
}

//DrawSelectedAppointment
if (selectedAppointment != null && selectedRectangle != Rectangle.Empty)
{
selectedRectangle.X -= 15;
selectedRectangle.Y -= 10;
selectedRectangle.Height += 20;
selectedRectangle.Width += 30;
renderer.DrawAppointment(e.Graphics, selectedRectangle, selectedAppointment, true, appointmentGripWidth);
selectedRectangle = Rectangle.Empty;
}
}

So I draw appointments after days. Also I redraw selected appointment and make it larger.
Don't forget remove call DrawAppointments() in DrawDay:

private void DrawDay(PaintEventArgs e, Rectangle rect, DateTime time)
{
...
// DrawAppointments(e, rect, time);
}


selectedRectangle I fill in DrawAppointment()

private void DrawAppointments(PaintEventArgs e, Rectangle rect, DateTime time)
{
...
if (drawnItems.IndexOf(appointment) < 0)
{
...
if (appointment == selectedAppointment)
selectedRectangle = appRect;
...
}
...
}

Here is my code (DayView.cs):
http://dl.dropbox.com/u/18180711/Calendar/DayView.cs