Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / C#

ExtendedDateTimePicker control with week numbers

Rate me:
Please Sign up or sign in to vote.
4.05/5 (10 votes)
14 Jan 2007CPOL 71.5K   23   21
Code for showing week numbers in a DateTimePicker control as the MonthCalendar control does.

Sample screenshot

Introduction

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.

Feedback

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

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

namespace WindowsApplication3
{
    public class ExtendedDateTimePicker : DateTimePicker
    {
        [DllImport("User32.dll")]
        private static extern int GetWindowLong(IntPtr h, int index);

        [DllImport("User32.dll")]
        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);

        [DllImport("User32.dll")]
        private static extern IntPtr SendMessage(IntPtr h, 
                       int msg, int param, int data);

        [DllImport("User32.dll")]
        private static extern int SendMessage(IntPtr h, int msg, 
                       int param, ref Rectangle data);

        [DllImport("User32.dll")]
        private static extern int MoveWindow(IntPtr h, int x, int y, 
                       int width, int height, bool repaint);

        private bool m_ShowWeekNumbers;

        [Browsable(true), DesignerSerializationVisibility(
                              DesignerSerializationVisibility.Visible)]
        public bool ShowWeekNumbers
        {
            get
            {
                return m_ShowWeekNumbers;
            }
            set
            {
                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;
            }
            else
            {
                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);
            base.OnDropDown(e);
        }
    }
}

License

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


Written By
Software Developer (Senior)
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow do the winform call this class ? Pin
tài nguyễn văn26-Feb-20 16:05
tài nguyễn văn26-Feb-20 16:05 
SuggestionConvert to VB.NET Pin
Wayne Jin2-Nov-16 4:35
Wayne Jin2-Nov-16 4:35 
VB.NET
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class ExtendedDateTimePicker : Inherits DateTimePicker
    Private Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal index As Int32) As Int32
    Private Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal index As Int32, ByVal value As Int32) As Int32

    Private Const GWL_STYLE As Int32 = (-16)
    Private Const MCM_FIRST As Int32 = &H1000
    Private Const MCM_GETMINREQRECT As Int32 = (MCM_FIRST + 9)
    Private Const MCS_WEEKNUMBERS As Int32 = &H4
    Private Const DTM_FIRST As Int32 = &H1000
    Private Const DTM_GETMONTHCAL As Int32 = (DTM_FIRST + 8)

    Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal msg As Int32, ByVal param As Int32, ByVal data As Int32) As IntPtr
    Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal msg As Int32, ByVal param As Int32, ByRef data As Rectangle) As Int32
    Private Declare Auto Function MoveWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal repaint As Boolean) As Int32

    Private _ShowWeekNumbers As Boolean

    <Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property ShowWeekNumbers() As Boolean
        Get
            Return _ShowWeekNumbers
        End Get
        Set(ByVal value As Boolean)
            _ShowWeekNumbers = value
        End Set
    End Property

    Private Const WS_BORDER As Int32 = &H800000
    Protected Overloads Overrides Sub OnDropDown(ByVal e As EventArgs)
        Dim hMonthView As IntPtr = SendMessage(Me.Handle, DTM_GETMONTHCAL, 0, 0)
        Dim hMonthViewParent As Int32 = GetWindowLong(hMonthView, -8)
        Dim dwStyle As Int32 = GetWindowLong(hMonthView, GWL_STYLE)
        If Me.ShowWeekNumbers Then
            dwStyle = dwStyle Or MCS_WEEKNUMBERS Or WS_BORDER
        Else
            dwStyle = dwStyle And Not MCS_WEEKNUMBERS
        End If
        Dim rct As New Rectangle
        SetWindowLong(hMonthView, GWL_STYLE, dwStyle)
        SendMessage(hMonthView, MCM_GETMINREQRECT, 0, rct)
        MoveWindow(hMonthViewParent, 0, 0, rct.Right, rct.Bottom, True)
        MoveWindow(hMonthView, 0, 0, rct.Right, rct.Bottom, True)
        MyBase.OnDropDown(e)
    End Sub
End Class


modified 2-Nov-16 10:49am.

QuestionMaxDate Pin
gmardok13-May-15 3:30
gmardok13-May-15 3:30 
AnswerRe: MaxDate Pin
Member 1507668717-Feb-21 21:53
Member 1507668717-Feb-21 21:53 
QuestionWindows 7 DTP Month FIX Pin
MaggYe2-Jul-13 3:53
MaggYe2-Jul-13 3:53 
AnswerRe: Windows 7 DTP Month FIX Pin
jBroenstrup19-Nov-15 23:43
jBroenstrup19-Nov-15 23:43 
GeneralReally Nice Pin
rapunsel11130-Apr-12 1:31
rapunsel11130-Apr-12 1:31 
SuggestionISO 8601 Pin
schub27-Feb-12 1:23
schub27-Feb-12 1:23 
GeneralRe: ISO 8601 Pin
_JERKER_1-Mar-12 6:08
_JERKER_1-Mar-12 6:08 
GeneralRe: ISO 8601 Pin
Member 427386310-Apr-12 2:42
Member 427386310-Apr-12 2:42 
GeneralRe: ISO 8601 Pin
Member 121093292-Nov-15 23:14
Member 121093292-Nov-15 23:14 
GeneralRe: ISO 8601 Pin
MeckyF7-Mar-16 22:49
MeckyF7-Mar-16 22:49 
GeneralMy vote of 5 Pin
wijnandnagelhout17-Nov-11 3:15
wijnandnagelhout17-Nov-11 3:15 
GeneralMy vote of 4 Pin
c_keen6-Nov-11 12:52
c_keen6-Nov-11 12:52 
GeneralSir Pin
Jx Cx2-Sep-09 22:33
Jx Cx2-Sep-09 22:33 
GeneralIs it that much complicated , see my work arround Pin
Member 48506911-Feb-09 21:14
Member 48506911-Feb-09 21:14 
RantProblem using this derived component Pin
Lagrange11-Aug-08 3:35
Lagrange11-Aug-08 3:35 
GeneralSend sample project Pin
jawahar srinivasan24-Mar-08 18:48
jawahar srinivasan24-Mar-08 18:48 
Questionwidth of dropdown part Pin
java4me13-Nov-07 23:33
java4me13-Nov-07 23:33 
GeneralRe: width of dropdown part Pin
rejuwi14-Dec-07 4:19
rejuwi14-Dec-07 4:19 
GeneralRe: width of dropdown part Pin
Pavel729-Sep-19 2:26
Pavel729-Sep-19 2:26 

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.