Click here to Skip to main content
13,288,520 members (59,705 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 16 Nov 2005

Customizable MonthCalendar Type Control: Part 2

, 16 Nov 2005
Rate this:
Please Sign up or sign in to vote.
A truly customizable MonthCalendar type control.

Sample Image

Screenshot showing appointment details.

Sample Image

Portion of the print preview window.


This is the second part of my article: Customizable MonthCalendar Type Control. The new capabilities added to the MonthCalendar control include:

  • Add appointments to the control. This will take the place of the BoldedDates array. Each appointment will contain a date, starting and ending times and a description. The date will appear bolded in the calendar; to view appointment info, right-click on the date.
  • Print capability for month calendar. The month grid and up to four appointments for each day will be printed.
  • Two new classes have been added to the calendar control, an appointment class and an appointment collection class. When the calendar is drawn, I check to see if any of the appointments match the date being drawn. If it does the date will be bolded. I wanted to display the appointment data without adding any other components to the calendar. To accomplish this, I display the appointment data in a context menu when the right mouse is pressed.
  • The process to print the calendar is very similar to the Paint routine, with the addition of four rectangles where the appointment information is drawn.


See part 1 of this article: Customizable MonthCalendar Type Control.

Using the code

The control may be used anywhere a standard MonthCalendar control would be used and it accepts an array of dates to be bolded. It may be sized to fit the space available.

Creating appointments:

private void Form1_Load(object sender, System.EventArgs e)
    mpK_Calendar1.SelectedDateChanged+= new 
    //part 2
    //At this point, the assumption is that 
    //appointments are added in chronological order
    mpK_Calendar1.Appointments.Add(new DateTime(2005,11,1,6,0,0), 
               new DateTime(2005,11,1,7,0,0),"TEST DESCRIPTION");
    mpK_Calendar1.Appointments.Add(new DateTime(2005,11,1,8,0,0),
               new DateTime(2005,11,1,9,0,0),"TEST DESCRIPTION");
    mpK_Calendar1.Appointments.Add(new DateTime(2005,11,1,10,0,0),
              new DateTime(2005,11,1,11,0,0),"TEST DESCRIPTION");

New appointment and appointment collection classes:

public class clsAppt
    public DateTime ApptStart;
    public DateTime ApptEnd;
    public string strDesc;

    public clsAppt(DateTime ApptStartIn, 
           DateTime ApptEndIn,string strDescIn)
        ApptStart = ApptStartIn;
        ApptEnd = ApptEndIn;
        strDesc = strDescIn;


public class clsApptColl:System.Collections.CollectionBase
    public clsApptColl()
    public void Add(clsAppt Appt)
    public void Add(DateTime ApptStartIn,
                DateTime ApptEndIn,string strDescIn)
        List.Add(new clsAppt(ApptStartIn,ApptEndIn,strDescIn));
    public void Remove(int intIndex)
        if(intIndex>-1 && intIndex<List.Count)
    public clsAppt Item(int intIndex)
        return (clsAppt) List[intIndex];
    public int ListCount()
        return List.Count;

Points of Interest

This is the code to dynamically create the context menu when the right-mouse is clicked:

//part 2
//A context menu is used to display the full details of each appointment.
//Based on the X,Y coordinates of the mouse, 
//check to see if there are any appointments to display.

//dynamically create context menu
ContextMenu contextMenu1 = new ContextMenu();
int intApptCtr=0;
if(e.Button==MouseButtons.Right)//right mouse
    for(int k=0;k<Appointments.ListCount();k++)
                                             new MenuItem("-"));
                 new MenuItem("Date: " + 
                 new MenuItem("Time: " + 
                 Appointments.Item(k).ApptStart.TimeOfDay + 
                 " - " + Appointments.Item(k).ApptEnd.TimeOfDay));
                 new MenuItem("Subject: " + 
            //show context menu
          contextMenu1.Show(this,new Point(e.X,e.Y));
else//left mouse

//part 2

When using GDI+ to paint forms or for printing, here are a couple of items I find useful.

When drawing text, I prefer to draw the text into a rectangle and use a StringFormat object to properly layout the text.

//string format for calendar body
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;

The Alignment property determines the left to right placement of the text within the rectangle. The LineAlignment property determines the up and down placement within the rectangle. The Trimming property determines what will happen if the text is too long for the rectangle.

If you need a standard color brush, you can create one:

new SolidBrush(Color.Black);

or use one from the Brushes class:



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Michael Krisky
Web Developer
United States United States
I've been developing software since 1995, using VB5, VB6, ASP, Javascript and most recently C# for ASP.NET and Windows. Current interests include embedded systems for robotics with the BASIC Stamp microcontroller from Parallax and the Brainstem module from Acroname.

You may also be interested in...


Comments and Discussions

QuestionGood Post Pin
Bhavesh Patel28-Jul-15 2:47
memberBhavesh Patel28-Jul-15 2:47 
GeneralCustomizable MonthCalendar Pin
zaferms28-Mar-11 4:15
memberzaferms28-Mar-11 4:15 
GeneralImportant Pin
eman.tabbara1-Dec-09 21:26
membereman.tabbara1-Dec-09 21:26 
GeneralNice but... Pin
chmod222211-Jul-07 1:52
memberchmod222211-Jul-07 1:52 
GeneralBolded Dates Pin
HomerGER20-Apr-07 12:42
memberHomerGER20-Apr-07 12:42 
GeneralPrintPreviewCalendar() modifies the DOW header sizing Pin
Burt Crockett20-Jul-06 12:15
memberBurt Crockett20-Jul-06 12:15 
Questionhow to change maxselectionnumber Pin
jabulino24-Nov-05 7:57
memberjabulino24-Nov-05 7:57 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 16 Nov 2005
Article Copyright 2005 by Michael Krisky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid