Click here to Skip to main content
Licence CPOL
First Posted 8 Jan 2007
Views 26,554
Bookmarked 21 times

ExtendedDateTimePicker control with week numbers

By | 14 Jan 2007 | Article
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

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)

About the Author

_JERKER_

Software Developer (Senior)

Sweden Sweden

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralReally Nice Pinmemberrapunsel1111:31 30 Apr '12  
SuggestionISO 8601 Pinmemberschub1:23 27 Feb '12  
GeneralRe: ISO 8601 Pinmember_JERKER_6:08 1 Mar '12  
GeneralRe: ISO 8601 PinmemberMember 42738632:42 10 Apr '12  
GeneralMy vote of 5 Pinmemberwijnandnagelhout3:15 17 Nov '11  
GeneralMy vote of 4 Pinmemberdragon_MF12:52 6 Nov '11  
GeneralSir PinmemberJx Cx22:33 2 Sep '09  
GeneralIs it that much complicated , see my work arround PinmemberMember 48506921:14 11 Feb '09  
RantProblem using this derived component PinmemberLagrange3:35 11 Aug '08  
GeneralSend sample project Pinmemberjawahar srinivasan18:48 24 Mar '08  
Questionwidth of dropdown part Pinmemberjava4me23:33 13 Nov '07  
GeneralRe: width of dropdown part Pinmemberrejuwi4:19 14 Dec '07  

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.

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