Click here to Skip to main content
15,899,754 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

 
Answercalendar contol Pin
sebymon16-May-08 22:32
sebymon16-May-08 22:32 
GeneralAppointmentDuration Pin
gkowaljow12-May-08 10:10
gkowaljow12-May-08 10:10 
GeneralNew Release Pin
Ertan Tike2-May-08 12:22
Ertan Tike2-May-08 12:22 
GeneralThanks a lot! Pin
karul24-Mar-08 13:07
karul24-Mar-08 13:07 
GeneralSee there Pin
RottenMuLoT11-Mar-08 17:22
RottenMuLoT11-Mar-08 17:22 
GeneralI cannot run it with vb.net Pin
goltra29-Feb-08 8:44
goltra29-Feb-08 8:44 
GeneralRe: I cannot run it with vb.net Pin
cinamon24-May-09 8:14
cinamon24-May-09 8:14 
GeneralGetting erros about Calendar.DayView. Please help me. Pin
0vermind15-Jan-08 13:59
0vermind15-Jan-08 13:59 
I am getting compilation errors, I modified the code a bit to include the database saving (someone on page two posted code to save the db in XML and then open it).

I'm not sure what I did to my code, (I followed everything he said and I have no errors in the spot where I added his coded but I have errors about Calander.Dayview not containing AppointmentViews.

Please help me!

Thanks in advance!!
-Mike

Here are the errors I am getting:
****************************************************<br />
Error	1	Using the generic type 'System.Collections.Generic.List<t>' requires '1' type arguments	C:\Users\Michael\Desktop\calendar_src\Calendar\Form1.cs	142	34	CalendarProgram<br />
<br />
Error	2	'Calendar.Appointment' does not contain a constructor that takes '1' arguments	C:\Users\Michael\Desktop\calendar_src\Calendar\Form1.cs	146	35	CalendarProgram<br />
<br />
Error	3	'Calendar.Form1' does not contain a definition for 'dataGridView1' and no extension method 'dataGridView1' accepting a first argument of type 'Calendar.Form1' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\Calendar\Form1.cs	164	18	CalendarProgram<br />
<br />
Error	4	'Calendar.Form1' does not contain a definition for 'dataGridView1' and no extension method 'dataGridView1' accepting a first argument of type 'Calendar.Form1' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\Calendar\Form1.cs	165	18	CalendarProgram<br />
<br />
Error	5	'Calendar.DayView' does not contain a definition for 'AppointmentViews' and no extension method 'AppointmentViews' accepting a first argument of type 'Calendar.DayView' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\DayView\SelectionTool.cs	133	25	DayView<br />
<br />
Error	6	'Calendar.DayView' does not contain a definition for 'appointmentViews' and no extension method 'appointmentViews' accepting a first argument of type 'Calendar.DayView' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\DayView\SelectionTool.cs	135	56	DayView<br />
<br />
Error	7	The name 'appointmentViews' does not exist in the current context	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	648	48	DayView<br />
<br />
Error	8	The name 'appointmentViews' does not exist in the current context	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	650	40	DayView<br />
<br />
Error	9	The name 'appointmentViews' does not exist in the current context	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	711	46	DayView<br />
<br />
Error	10	Using the generic type 'System.Collections.Generic.List<t>' requires '1' type arguments	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	856	17	DayView<br />
<br />
Error	11	Using the generic type 'System.Collections.Generic.List<t>' requires '1' type arguments	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	856	35	DayView<br />
<br />
Error	12	The name 'DrawAppointments' does not exist in the current context	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	873	21	DayView<br />
<br />
Error	13	'Appointment' does not contain a definition for 'm_ConflictCount' and no extension method 'm_ConflictCount' accepting a first argument of type 'Appointment' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	888	29	DayView<br />
<br />
Error	14	'Appointment' does not contain a definition for 'm_ConflictCount' and no extension method 'm_ConflictCount' accepting a first argument of type 'Appointment' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	914	38	DayView<br />
<br />
Error	15	'Appointment' does not contain a definition for 'm_ConflictCount' and no extension method 'm_ConflictCount' accepting a first argument of type 'Appointment' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	915	38	DayView<br />
<br />
Error	16	The name 'appointmentViews' does not exist in the current context	C:\Users\Michael\Desktop\calendar_src\DayView\DayView.cs	961	13	DayView<br />
<br />
******************************************************************</t></t></t>

GeneralThanks Pin
Stradinov14-Jan-08 17:10
Stradinov14-Jan-08 17:10 
GeneralIssue with multi-day Pin
RUrry10-Jan-08 12:43
RUrry10-Jan-08 12:43 
NewsFix for moving all-day appointments to normal view Pin
SohailB18-Dec-07 1:40
SohailB18-Dec-07 1:40 
Questionstoring title in db Pin
alphasigma30-Nov-07 12:56
alphasigma30-Nov-07 12:56 
Generalnew appointment Pin
NewToAspDotNet4-Nov-07 0:43
NewToAspDotNet4-Nov-07 0:43 
GeneralRe: new appointment Pin
Snerf20-Nov-07 12:41
Snerf20-Nov-07 12:41 
Questionappoinments to db? Pin
NewToAspDotNet3-Nov-07 1:34
NewToAspDotNet3-Nov-07 1:34 
AnswerRe: appoinments to db? Pin
Snerf20-Nov-07 12:39
Snerf20-Nov-07 12:39 
GeneralAppointmentMove event Pin
zwipher2-Nov-07 6:20
zwipher2-Nov-07 6:20 
GeneralRe: AppointmentMove event Pin
Snerf20-Nov-07 12:42
Snerf20-Nov-07 12:42 
GeneralHorizontal lines in Office 12 View Pin
Aicho8422-Oct-07 4:02
Aicho8422-Oct-07 4:02 
GeneralRe: Horizontal lines in Office 12 View Pin
Claus7T23-Oct-07 21:37
Claus7T23-Oct-07 21:37 
GeneralGroup by name Pin
Wallace8416-Oct-07 22:25
Wallace8416-Oct-07 22:25 
GeneralRe: Group by name Pin
DeepToot9-Nov-07 6:41
DeepToot9-Nov-07 6:41 
GeneralRe: Group by name Pin
DeepToot9-Nov-07 9:45
DeepToot9-Nov-07 9:45 
QuestionRe: Group by name Pin
wc01710-Sep-08 5:43
wc01710-Sep-08 5:43 
AnswerRe: Group by name Pin
DeepToot10-Sep-08 6:00
DeepToot10-Sep-08 6:00 

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.