Xceed Grid for WinForms v4.3 Documentation
CustomCalendar C#
Welcome to Xceed Grid for WinForms v4.3 > Advanced Concepts > Editors - Advanced Concepts > Custom painting the WinCalendar control > CustomCalendar C#

The following code provides the C# implementation of the CustomCalendar class.

C# Copy Code

using System;
using System.Drawing;
using System.Collections;
using Xceed.Editors;

namespace Xceed.Editors.Samples
{
  public class CustomCalendar : WinCalendar
  {
    public CustomCalendar( ArrayList specialOccasions )
     : base( true, true )
    {
      m_specialOccasions = specialOccasions;
      this.Width = this.Width * 2;
      this.Height = this.Height * 2;
    }

    public ArrayList SpecialOccasions
    {
      get
      {
        return m_specialOccasions;
      }
    }   

    protected override bool DefaultShowPreviewDays
    {
      get
      {
        return false;
      }
    } 

    protected override void OnPaintDayBackground( PaintDayEventArgs e )
    {
      base.OnPaintDayBackground( e );

      foreach( SpecialOccasion occasion in m_specialOccasions )
      {
        if( ( occasion.Date == e.Value && occasion.RepeatPattern == RepeatPattern.Once ) ||
            ( occasion.RepeatPattern == RepeatPattern.EveryDay ) ||
            ( ( occasion.Date.DayOfWeek == e.Value.DayOfWeek ) &&
            occasion.RepeatPattern == RepeatPattern.EveryWeek ) ||
            ( ( occasion.Date.Day == e.Value.Day ) &&
            occasion.RepeatPattern == RepeatPattern.EveryMonth ) ||
            ( ( occasion.Date.Month == e.Value.Month && occasion.Date.Day == e.Value.Day ) &&
            occasion.RepeatPattern == RepeatPattern.EveryYear ) )
        {
          using( Bitmap image = new Bitmap( occasion.ImagePath ) )
          {
            e.Graphics.DrawImage( image, e.ClipRectangle );
          }
        }
      }   
    }

    protected override void OnPaintDay( PaintDayEventArgs e )
    {
      foreach( SpecialOccasion occasion in m_specialOccasions )
      {
        if( ( occasion.Date == e.Value && occasion.RepeatPattern == RepeatPattern.Once ) ||
            ( occasion.RepeatPattern == RepeatPattern.EveryDay ) ||
            ( ( occasion.Date.DayOfWeek == e.Value.DayOfWeek ) &&
            occasion.RepeatPattern == RepeatPattern.EveryWeek ) ||
            ( ( occasion.Date.Day == e.Value.Day ) &&
            occasion.RepeatPattern == RepeatPattern.EveryMonth ) ||
            ( ( occasion.Date.Month == e.Value.Month && occasion.Date.Day == e.Value.Day ) &&
            occasion.RepeatPattern == RepeatPattern.EveryYear ) )
        {
          if( occasion.PaintText )  
          {
            base.OnPaintDay( e );
          }         
          return;
        }
      }
      base.OnPaintDay( e );
    }   

    private ArrayList m_specialOccasions = null;
  }

  public class SpecialOccasion
  {
    public SpecialOccasion( DateTime date, string imagePath, RepeatPattern repeatPattern, bool paintText )
    {     
      if( imagePath == string.Empty )
        throw new ArgumentNullException( "imagePath", "A path to an image must be provided" );

      m_date = date;
      m_imagePath = imagePath;     
      m_repeatPattern = repeatPattern;
      m_paintText = paintText;
    }   

    public DateTime Date
    {
      get{ return m_date; }
    }

    public string ImagePath
    {
      get{ return m_imagePath; }
    }

    public RepeatPattern RepeatPattern
    {
      get{ return m_repeatPattern; }
    }

    public bool PaintText
    {
      get{ return m_paintText; }
    }   

    private RepeatPattern m_repeatPattern = RepeatPattern.Once;
    private DateTime m_date = DateTime.MinValue;
    private string m_imagePath = string.Empty;  
    private bool m_paintText = true;   
  }

  public enum RepeatPattern
  {
    Once,
    EveryDay,
    EveryWeek,
    EveryMonth,
    EveryYear
  }
}