SHARE
TWEET

Untitled

a guest Jan 12th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace CW
  5. {
  6.     public partial class DateTimePickerEx1 : DateTimePicker
  7.     {
  8.         #region Constructor
  9.         public DateTimePickerA4Ex1()
  10.         {
  11.             InitializeComponent();
  12.  
  13.  
  14.         }
  15.         #endregion Constructor
  16.  
  17.         private bool selectionComplete = false;
  18.         private bool numberKeyPressed = false;
  19.  
  20.         private const int WM_KEYUP = 0x0101;
  21.         private const int WM_KEYDOWN = 0x0100;
  22.         private const int WM_REFLECT = 0x2000;
  23.         private const int WM_NOTIFY = 0x004e;
  24.  
  25.         [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
  26.         private struct NMHDR
  27.         {
  28.             public IntPtr hwndFrom;
  29.             public IntPtr idFrom;
  30.             public int Code;
  31.         }
  32.         protected override void OnKeyDown(KeyEventArgs e)
  33.         {
  34.             numberKeyPressed = (e.Modifiers == Keys.None && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode != Keys.Back && e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)));
  35.             selectionComplete = false;
  36.             base.OnKeyDown(e);
  37.         }
  38.         protected override void WndProc(ref Message m)
  39.         {
  40.             if (m.Msg == WM_REFLECT + WM_NOTIFY)
  41.             {
  42.                 var hdr = (NMHDR)m.GetLParam(typeof(NMHDR));
  43.                 if (hdr.Code == -759) //date chosen (by keyboard)
  44.                     selectionComplete = true;
  45.             }
  46.             base.WndProc(ref m);
  47.         }
  48.         protected override void OnKeyUp(KeyEventArgs e)
  49.         {
  50.             base.OnKeyUp(e);
  51.             if (numberKeyPressed && selectionComplete &&
  52.                 (e.Modifiers == Keys.None && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode != Keys.Back && e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))))
  53.             {
  54.                 Message m = new Message();
  55.                 m.HWnd = this.Handle;
  56.                 m.LParam = IntPtr.Zero;
  57.                 m.WParam = new IntPtr((int)Keys.Right); //right arrow key
  58.                 m.Msg = WM_KEYDOWN;
  59.                 base.WndProc(ref m);
  60.                 m.Msg = WM_KEYUP;
  61.                 base.WndProc(ref m);
  62.                 numberKeyPressed = false;
  63.                 selectionComplete = false;
  64.             }
  65.         }
  66.     }
  67. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top