Click here to Skip to main content
15,886,006 members
Articles / Desktop Programming / MFC

Calendar DayView Control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (152 votes)
20 Jun 2006CPOL3 min read 1.7M   24.9K   375   510
A calendar DayView control.

Introduction

This article describes creating a day view control for visualizing schedule functions in required applications. I have cloned the Outlook day view appearance for similar use. Here is a screenshot:

Office 12 Theme:

Sample Image

Office XP Theme:

Sample Image

Background

Before writing this control, I was in need of a day view control that just looks like that in Outlook. I have found some commercial toolkits but none of them meets my requirements. Some of those want that all appointments be given before showing the control, some of those are not open source, etc. So I wrote this control in a "hurry development", and I think other people can use it. Pay back time for using the CodeProject :)

What we have?

  • You can create your appointment class to hold special information (other than start, end dates and title).
  • You don't need to read all appointments from the DB or something like that.
  • You can specify how much days will be shown.
  • You can colorize appointments to show different views.
  • In-place editing.
  • Drag drop operations.
  • No Win32 API.
  • Theme based rendering.

By the way, it's compiled under the final release of .NET 2.0. If you don't have it, you need to recompile the project.

Using the code

This control uses a class named "Appointment" to visualize the view. The DayView control doesn't pay attention to saving appointments to the DB or fetching them. So, you need to write your own DB logic and answer the events.

The control implements these events to interact with the hosting application:

  • dayView1.NewAppointment
  • dayView1.ResolveAppointments
  • dayView1.SelectionChanged

The sample application uses a list collection as the container for appointments. You may use a cached DB source too.

NewAppointment event

This is raised when the user wants to create an appointment. Event arguments contain start date, end date, and title values of the new appointment. You can create your appointment class that inherits from the DayView.Appointment base class.

The sample application just creates a new appointment, and adds it to the list collection.

C#
void dayView1_NewAppointment(object sender, NewAppointmentEventArgs args)
{
    Appointment m_Appointment = new Appointment();

    m_Appointment.StartDate = args.StartDate;
    m_Appointment.EndDate = args.EndDate;
    m_Appointment.Title = args.Title;

    m_Appointments.Add(m_Appointment);
}

ResolveAppointments event

This event is raised when the DayView control needs to show an appointment on a date. Event arguments contain the start date and end date of the required range of dates.

The sample application scans the list collection for a specified date range. You can fetch them from your own DB too.

C#
private void dayView1_ResolveAppointments(object sender, 
                      ResolveAppointmentsEventArgs args)
{
    List<Appointment> m_Apps = new List<Appointment>();

    foreach (Appointment m_App in m_Appointments)
        if ((m_App.StartDate >= args.StartDate) && 
            (m_App.StartDate <= args.EndDate))
            m_Apps.Add(m_App);

    args.Appointments = m_Apps;
}

Selection Changed event

This event is raised when the user selects an appointment.

C#
private void dayView1_SelectionChanged(object sender, EventArgs e)
{
    label3.Text = dayView1.SelectionStart.ToString() + 
                  ":" + dayView1.SelectionEnd.ToString();
}

The sample application shows the start date and the end date of the selected appointment in a label.

Points of interest

When I wrote this control, the hard part was sorting the appointments on screen without crossing over. The control internally uses an "AppointmentView" class to hold the state of appointments on screen. The rest of the code was drawing and isolating from the external application.

You may see some remarked codes about all-day events, but currently all-day events is not complete. When I finish it, I'll update this article. (Thanks to Claus Espersen for implementing the Office 12 theme and bug fixes.)

History

  • 14.07.2006
    • Bug fixes.
    • Start hour and start minute properties implemented.
    • Theme based rendering implemented.
    • AllowNew property implemented.
    • Mouse drag drop bugs fixed.
    • Internal changes for all day events.
    • Zoom feature implemented.
  • 11.09.2005
    • Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDelete NewAppointment? Pin
Pato de Borracfha23-Oct-11 1:57
Pato de Borracfha23-Oct-11 1:57 
QuestionWeb control Pin
PrincVenu10-Oct-11 22:21
PrincVenu10-Oct-11 22:21 
QuestionNice One Pin
Saqlain Raza7-Oct-11 4:41
Saqlain Raza7-Oct-11 4:41 
GeneralMy vote of 3 Pin
buyong26-Sep-11 20:34
buyong26-Sep-11 20:34 
NewsAppointmentID is back Pin
nt238229-Aug-11 8:10
nt238229-Aug-11 8:10 
QuestionRe: AppointmentID is back Pin
marcopuccio2-Jan-13 23:38
marcopuccio2-Jan-13 23:38 
GeneralRe: AppointmentID is back Pin
MarioRDJ9-Jul-15 0:18
MarioRDJ9-Jul-15 0:18 
NewsMark more then one day in the calender Pin
nt238229-Aug-11 8:02
nt238229-Aug-11 8:02 
in DayView.cs in

private void DrawDay(PaintEventArgs e, Rectangle rect, DateTime time) if ((selection ==

insert the text between

if (!((time.DayOfWeek == DayOfWeek.Saturday) || (time.DayOfWeek == DayOfWeek.Sunday))) //weekends off -> no working hours
renderer.DrawHourRange(e.Graphics, workingHoursRectangle, false, false);
and

e.Graphics.SetClip(rect);

and replace the old text

the 8 is while the workhour begins with 8. the 15 while the workhour ends with 18.
this numbers can change.

---> the text
SelectionType.DateRange ) && (time.Day == selectionStart.Day || selectionStart.Day < selectionEnd.Day))
{
if (selectionStart.Day < selectionEnd.Day)
{
if (time.Day == selectionStart.Day)
{
Rectangle selectionRectangle = GetHourRangeRectangle(selectionStart, time.AddHours(15), rect);
if (selectionRectangle.Top + 1 > this.HeaderHeight)
renderer.DrawHourRange(e.Graphics, selectionRectangle, false, true);
}
if (time.Day > selectionStart.Day && time.Day < selectionEnd.Day)
{
Rectangle selectionRectangle = GetHourRangeRectangle(time.AddHours(8), time.AddHours(15), rect);
if (selectionRectangle.Top + 1 > this.HeaderHeight)
renderer.DrawHourRange(e.Graphics, selectionRectangle, false, true);
}
if (time.Day == selectionEnd.Day)
{
Rectangle selectionRectangle = GetHourRangeRectangle(time.AddHours(8), selectionEnd, rect);
if (selectionRectangle.Top + 1 > this.HeaderHeight)
renderer.DrawHourRange(e.Graphics, selectionRectangle, false, true);
}
}
else
{
if (time.Day == selectionStart.Day && time.Day == selectionEnd.Day)
{
Rectangle selectionRectangle = GetHourRangeRectangle(selectionStart, selectionEnd, rect);
if (selectionRectangle.Top + 1 > this.HeaderHeight)
renderer.DrawHourRange(e.Graphics, selectionRectangle, false, true);
}
}
}
NewsGrip on a wrong position [modified] Pin
nt238227-Aug-11 2:06
nt238227-Aug-11 2:06 
QuestionUsing With Database Pin
Mehmet Yılmaz Yılmaz14-Jul-11 5:52
Mehmet Yılmaz Yılmaz14-Jul-11 5:52 
GeneralPopulating the control Pin
Member 79015268-May-11 23:32
Member 79015268-May-11 23:32 
Generaldatebase problem i need help Pin
osman osmanssson8-May-11 7:35
osman osmanssson8-May-11 7:35 
GeneralProblem: X displayed Pin
Cucca7728-Apr-11 8:00
Cucca7728-Apr-11 8:00 
GeneralFont, BackColor, Padding Pin
Konrad8420-Apr-11 0:09
Konrad8420-Apr-11 0:09 
GeneralAll Day Events Pin
Nicoel18-Apr-11 9:26
Nicoel18-Apr-11 9:26 
QuestionAmazing work... Pin
DavidSavage9-Mar-11 11:55
DavidSavage9-Mar-11 11:55 
GeneralLatest version Pin
algreat25-Jan-11 0:49
algreat25-Jan-11 0:49 
GeneralRe: Latest version Pin
kiloolik120-Mar-11 7:30
kiloolik120-Mar-11 7:30 
GeneralBig problem with appointment rendering Pin
lbarciko16-Dec-10 20:36
lbarciko16-Dec-10 20:36 
GeneralAbsolutely brilliant Pin
Vidhu Jain2-Dec-10 5:01
Vidhu Jain2-Dec-10 5:01 
QuestionHow to test Appointment exist Pin
hoangsamac12-Oct-10 0:38
hoangsamac12-Oct-10 0:38 
QuestionHow to test Appointment exist ? please help me. Pin
hoangsamac12-Oct-10 0:37
hoangsamac12-Oct-10 0:37 
GeneralMy vote of 5 Pin
Lance.Zhang18-Aug-10 20:51
Lance.Zhang18-Aug-10 20:51 
GeneralNeed Printing help Pin
mindserve16-Aug-10 0:34
mindserve16-Aug-10 0:34 
GeneralPrinting Pin
mindserve5-Aug-10 1:31
mindserve5-Aug-10 1:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.