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

 
GeneralHelp with Calendar dayview control and vb.net Pin
bbepristis27-Oct-06 20:00
bbepristis27-Oct-06 20:00 
GeneralRe: Help with Calendar dayview control and vb.net Pin
Ertan Tike28-Oct-06 1:10
Ertan Tike28-Oct-06 1:10 
QuestionUse the component with data from a DB Pin
dbarquero24-Oct-06 8:37
dbarquero24-Oct-06 8:37 
AnswerRe: Use the component with data from a DB Pin
gregchak25-Oct-06 16:49
gregchak25-Oct-06 16:49 
GeneralAM PM Time Code - Non-Military Pin
pracsec2-Oct-06 7:58
pracsec2-Oct-06 7:58 
GeneralRe: AM PM Time Code - Non-Military Pin
Ertan Tike28-Oct-06 1:11
Ertan Tike28-Oct-06 1:11 
GeneralThis control is great !!!! Pin
labroze27-Sep-06 8:01
labroze27-Sep-06 8:01 
Generalhour Label Pin
hijazy.mahdy27-Sep-06 2:55
hijazy.mahdy27-Sep-06 2:55 
Hi,
just to make it looks more like MS outlook you can add this to the code of DrawHourLable (Render Files) and it will draw (highlight) half hour of the current time like in outlook it self
and in order to have more control on this label you can split the height (rect)to half hours in the function DrawHourLable (DayView.cs)
hourRectangle.Height = halfHourHeight;

Render Files
public override void DrawHourLabel(System.Drawing.Graphics g, System.Drawing.Rectangle rect, int hour)
{
Color color = Color.FromArgb(101, 147, 207);
DateTime now = DateTime.Now;
System.Drawing.Rectangle gradientRect = rect;
//highlite the current time
if (now.Hour == hour )
{
gradientRect.Y += (now.Minute <=30)? 0: rect.Height;
LinearGradientBrush lgb = new LinearGradientBrush(gradientRect, SystemColors.Control, Color.Orange, LinearGradientMode.Vertical);
g.FillRectangle(lgb, gradientRect);

g.DrawLine(new Pen(Brushes.DarkOrange), gradientRect.Left, gradientRect.Bottom, gradientRect.Right, gradientRect.Bottom);
}



Mahdy Hijazy
CC expert
GeneralRe: hour Label Pin
Claus7T28-Sep-06 0:24
Claus7T28-Sep-06 0:24 
QuestionCannot set appointment in the 23:30 - 00:00 range? Pin
johnwharrison7128326-Sep-06 20:22
johnwharrison7128326-Sep-06 20:22 
GeneralHELP PLEASE Pin
B-NiTo18-Sep-06 16:41
B-NiTo18-Sep-06 16:41 
QuestionNeed a Delete Appointment function [modified] Pin
Goering14-Sep-06 4:08
Goering14-Sep-06 4:08 
AnswerRe: Need a Delete Appointment function Pin
SohailB4-Nov-06 3:22
SohailB4-Nov-06 3:22 
GeneralConflict for 5, 15 minutes... Pin
Stailer658-Sep-06 22:57
Stailer658-Sep-06 22:57 
GeneralRe: Conflict for 5, 15 minutes... Pin
gregchak28-Sep-06 8:11
gregchak28-Sep-06 8:11 
GeneralHelp Pin
obinna_eke5-Sep-06 0:54
obinna_eke5-Sep-06 0:54 
QuestionUpdated source? Pin
gregchak30-Aug-06 13:10
gregchak30-Aug-06 13:10 
AnswerRe: Updated source? Pin
Ertan Tike31-Aug-06 6:18
Ertan Tike31-Aug-06 6:18 
GeneralRe: Updated source? Pin
gregchak1-Sep-06 6:17
gregchak1-Sep-06 6:17 
GeneralRe: Updated source? Pin
Stailer651-Sep-06 9:18
Stailer651-Sep-06 9:18 
GeneralShowing short periods Pin
sagaert27-Aug-06 2:52
sagaert27-Aug-06 2:52 
GeneralRe: Showing short periods Pin
igca7-Sep-06 6:08
igca7-Sep-06 6:08 
GeneralRe: Showing short periods Pin
igca7-Sep-06 6:08
igca7-Sep-06 6:08 
GeneralThanks for the article Pin
Quentin Pouplard26-Aug-06 0:50
Quentin Pouplard26-Aug-06 0:50 
GeneralA couple of bugs Pin
ahatzig20-Aug-06 7:13
ahatzig20-Aug-06 7:13 

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.