Click here to Skip to main content
6,631,404 members and growing! (15,610 online)
Email Password   helpLost your password?
Desktop Development » Selection Controls » Date/Time controls     Intermediate License: The Code Project Open License (CPOL)

Calendar DayView Control

By Ertan Tike

A calendar DayView control.
C#, VC6.NET 2.0, MFC, GDI, VS2005, Dev
Posted:9 Nov 2005
Updated:20 Jun 2006
Views:278,254
Bookmarked:283 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
123 votes for this article.
Popularity: 9.82 Rating: 4.70 out of 5
2 votes, 1.6%
1
1 vote, 0.8%
2

3
16 votes, 13.0%
4
104 votes, 84.6%
5

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.

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.

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.

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)

About the Author

Ertan Tike


Member

Occupation: Web Developer
Location: Turkey Turkey

Other popular Selection Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 397 (Total in Forum: 397) (Refresh)FirstPrevNext
GeneralPlease help db Pinmembereman.tabbara21:30 20 Nov '09  
GeneralThank you Pinmembernbize11:21 15 Nov '09  
General5 min time slots PinmemberTrevorWJ0:53 3 Nov '09  
Questionhow can update my data when i change an appoinment? PinmemberMiguel Angel BC12:33 14 Oct '09  
AnswerRe: how can update my data when i change an appoinment? Pinmemberchenliang.xf22:52 1 Nov '09  
GeneralExcellent Pinmemberdhoogsteen8:38 14 Oct '09  
QuestionMulti Day Appointments PinmemberChrisElston5:50 12 Oct '09  
GeneralBugfix - Crash when over 20 appointments at same time PinmemberJiong Mai5:17 25 Sep '09  
Questionusing a database rather than a list PinmemberAn Enigma3:08 24 Sep '09  
AnswerRe: using a database rather than a list Pinmemberchenliang.xf22:37 1 Nov '09  
GeneralVery impressed. Pinmemberanarchistic0:26 24 Sep '09  
GeneralAwesome Control!! PinmemberMJPreuss5:39 23 Sep '09  
JokeVery Nice Control Pinmemberjeancabral9:53 14 Sep '09  
GeneralAdding Control Values to Appointment Pinmemberk096:51 4 Sep '09  
GeneralRe: Adding Control Values to Appointment Pinmemberchenliang.xf22:44 1 Nov '09  
GeneralDelete Appointment Pinmemberk094:44 4 Sep '09  
GeneralRe: Delete Appointment Pinmemberprofeet2:20 12 Sep '09  
QuestionSwitch view and selection to the next appointment PinmemberEJOC4:32 27 Aug '09  
GeneralHelp people PinmemberMember 391685611:17 5 Aug '09  
GeneralRe: Help people Pinmemberchenliang.xf22:48 1 Nov '09  
GeneralDayView control for webforms PinmemberMutalib Salami11:10 4 Aug '09  
QuestionMidnight Bug PinmemberJason Barrera10:32 30 Jul '09  
GeneralBug and my fixture #2 Pinmembergeneral.jesiu3:33 16 Jul '09  
GeneralBug and my fixture [modified] Pinmembergeneral.jesiu2:23 16 Jul '09  
QuestionBug - Appointment jumps to fullday section / dissapears on click : Please help? [modified] Pinmemberjanniebun22:33 8 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jun 2006
Editor: Smitha Vijayan
Copyright 2005 by Ertan Tike
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project