Click here to Skip to main content
11,802,442 members (72,576 online)
Click here to Skip to main content

ExtendedDateTimePicker control with week numbers

, 14 Jan 2007 CPOL 39K 23
Rate this:
Please Sign up or sign in to vote.
Code for showing week numbers in a DateTimePicker control as the MonthCalendar control does.

Sample screenshot


My mission was to find a DateTimePicker control supporting week numbers. This was not as easy as I'd expected it to be...

After having Googled all day (almost), and obviously not finding any code here at The Code Project, I found a bit of VB code that helped me further... However, as all my components are written in C#, I started "translating" it. This article simply enlists the code in C#. The original VB code under the original article "Displaying week numbers in a DateTimePicker control's dropdown part" by Herfried K. Wagnercan, can be found here.


Feel free to report any errors or further thoughts about the code! The code lacks comments, but is quite self-explanatory for an intermediate programmer with some experience of Win32.

The Code

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsApplication3
    public class ExtendedDateTimePicker : DateTimePicker
        private static extern int GetWindowLong(IntPtr h, int index);

        private static extern int SetWindowLong(IntPtr h, int index, int value);

        private const int GWL_STYLE = (-16);
        private const int MCM_FIRST = 0x1000;
        private const int MCM_GETMINREQRECT = (MCM_FIRST + 9);
        private const int MCS_WEEKNUMBERS = 0x4;
        private const int DTM_FIRST = 0x1000;
        private const int DTM_GETMONTHCAL = (DTM_FIRST + 8);

        private static extern IntPtr SendMessage(IntPtr h, 
                       int msg, int param, int data);

        private static extern int SendMessage(IntPtr h, int msg, 
                       int param, ref Rectangle data);

        private static extern int MoveWindow(IntPtr h, int x, int y, 
                       int width, int height, bool repaint);

        private bool m_ShowWeekNumbers;

        [Browsable(true), DesignerSerializationVisibility(
        public bool ShowWeekNumbers
                return m_ShowWeekNumbers;
                m_ShowWeekNumbers = value;

        protected override void OnDropDown(EventArgs e)
            IntPtr monthView = SendMessage(Handle, DTM_GETMONTHCAL, 0, 0);
            int style = GetWindowLong(monthView, GWL_STYLE);
            if (ShowWeekNumbers)
                style = style | MCS_WEEKNUMBERS;
                style = style & ~MCS_WEEKNUMBERS;
            Rectangle rect = new Rectangle();
            SetWindowLong(monthView, GWL_STYLE, style);
            SendMessage(monthView, MCM_GETMINREQRECT, 0, ref rect);
            MoveWindow(monthView, 0, 0, rect.Right + 2, rect.Bottom, true);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionMaxDate Pin
gmardok13-May-15 3:30
membergmardok13-May-15 3:30 
QuestionWindows 7 DTP Month FIX Pin
MaggYe2-Jul-13 3:53
memberMaggYe2-Jul-13 3:53 
GeneralReally Nice Pin
rapunsel11130-Apr-12 1:31
memberrapunsel11130-Apr-12 1:31 
SuggestionISO 8601 Pin
schub27-Feb-12 1:23
memberschub27-Feb-12 1:23 
GeneralRe: ISO 8601 Pin
_JERKER_1-Mar-12 6:08
member_JERKER_1-Mar-12 6:08 
GeneralRe: ISO 8601 Pin
Member 427386310-Apr-12 2:42
memberMember 427386310-Apr-12 2:42 
GeneralMy vote of 5 Pin
wijnandnagelhout17-Nov-11 3:15
memberwijnandnagelhout17-Nov-11 3:15 
GeneralMy vote of 4 Pin
dragon_MF6-Nov-11 12:52
memberdragon_MF6-Nov-11 12:52 
GeneralSir Pin
Jx Cx2-Sep-09 22:33
memberJx Cx2-Sep-09 22:33 
GeneralIs it that much complicated , see my work arround Pin
Member 48506911-Feb-09 21:14
memberMember 48506911-Feb-09 21:14 
I do not think it is that much comlicated

see my work arround for displying week number

Swich on custom format in the desinger

Put this code in form load


add this method for value changed event

sub DTPWeek1_ValueChanged ( ...) handles DTPWeek1.ValueChanged

DTPWeek1.CustomFormat= "'" + DatePart(DateInterval.Year ,DTPWeek1.value ).Tostring & " week " & DatePart(DateInterval.WeekofYear ,DTPWeek1.value ).Tostring & "'"

end sub

And this method is known as using body text Datetime picker control and it is a must body
To be inside quotes like 'bodytext'
Smile | :)
RantProblem using this derived component Pin
Lagrange11-Aug-08 3:35
memberLagrange11-Aug-08 3:35 
GeneralSend sample project Pin
jawahar srinivasan24-Mar-08 18:48
memberjawahar srinivasan24-Mar-08 18:48 
Questionwidth of dropdown part Pin
java4me13-Nov-07 23:33
memberjava4me13-Nov-07 23:33 
GeneralRe: width of dropdown part Pin
rejuwi14-Dec-07 4:19
memberrejuwi14-Dec-07 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 15 Jan 2007
Article Copyright 2007 by _JERKER_
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid