[转载]TDateEditPicker: A Nullable & Bindingable Date Edit and Pick Control

(原创文章,转载请注明来源:http://blog.csdn.net/hulihui/article/details/5376251)  

 

 

[转载]TDateEditPicker: A Nullable & Bindingable Date Edit and Pick Control_第1张图片[转载]TDateEditPicker: A Nullable & Bindingable Date Edit and Pick Control_第2张图片

TDateEditPicker is a .net 2.0 windows form control which is derived from TextBox and DateTimePicker, it's main features are:

  • Date can be nullable and bindingable.(ver1.3)
  • Can set the edit box ForeColor and BackColor.
  • Can set the error date ForeColor.
  • Click/Enter selects a date.
  • ESC/Click outside control abandons the current select.
  • Can set initiate date when at design mode.(ver1.5)
  • Can set date show format(yyyymmdd/mmddyy/ddmmyyyy).
  • Can set date seperator.

 Below are the source codes of TDateEditPicer(Ver 1.3): TDateEditPicker Demo and Sources, Ver1.5: TDateEditPicker Demo and Source.,Ver1.6: TDateEditPicker Demo and Source。Ver1.8: TDateEditPicker Demo and Source. Version1.9(2010-4-8)。Version1.11(2010-4-17)。

 

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Text.RegularExpressions;  
  6.   
  7. namespace CSUST.Data  
  8. {  
  9.     [ToolboxItem(false)]  
  10.     public class TDateEditBox : TextBox  
  11.     {  
  12.         private const int MaxTextLength = 10;    // 固定10个字符   
  13.   
  14.         private char[] textChars = new char[] { ' '' '' '' ''-'' '' ''-'' '' ' };  
  15.         private char[] lastErrorTextChars = new char[MaxTextLength];  
  16.   
  17.         private char dateSeperator = '-';  // 分隔   
  18.   
  19.         private int minDateYear = 1950;  
  20.         private int maxDateYear = 2060;  
  21.   
  22.         private bool isNull = true;  
  23.         private bool isValid = false;  
  24.   
  25.         private Color validDateForeColor;  
  26.         private Color errorDateForeColor = Color.Red;  
  27.   
  28.         private DateTime date;  
  29.   
  30.         public event EventHandler EditBoxDateChanged;  
  31.   
  32.         public TDateEditBox()  
  33.         {  
  34.             base.MaxLength = MaxTextLength;  
  35.             this.ContextMenu = new ContextMenu();  
  36.             validDateForeColor = base.ForeColor;  
  37.             this.SetToNullDate();  
  38.         }  
  39.   
  40.         public new int MaxLength  
  41.         {  
  42.             get  
  43.             {  
  44.                 return base.MaxLength;  
  45.             }  
  46.             set  
  47.             {  
  48.                 base.MaxLength = MaxTextLength;  
  49.             }  
  50.         }  
  51.   
  52.         public int MinDateYear  
  53.         {  
  54.             get { return minDateYear; }  
  55.             set { minDateYear = value; }  
  56.         }  
  57.   
  58.         public int MaxDateYear  
  59.         {  
  60.             get { return maxDateYear; }  
  61.             set { maxDateYear = value; }  
  62.         }  
  63.   
  64.         public Color ErrorDateForeColor  
  65.         {  
  66.             get { return errorDateForeColor; }  
  67.             set  
  68.             {  
  69.                 errorDateForeColor = value;  
  70.                 if (isValid == false && isNull == false)  
  71.                 {  
  72.                     this.ShowErrorDateColor();  
  73.                 }  
  74.             }  
  75.         }  
  76.   
  77.         public override Color ForeColor  
  78.         {  
  79.             get { return base.ForeColor; }  
  80.             set  
  81.             {  
  82.                 if (value != errorDateForeColor)  
  83.                 {  
  84.                     base.ForeColor = value;  
  85.                     validDateForeColor = value;  
  86.                 }  
  87.                 if (isNull == true || isValid == true)  
  88.                 {  
  89.                     this.ShowValidDateColor();  
  90.                 }  
  91.             }  
  92.         }  
  93.   
  94.         public char DateSeperator  
  95.         {  
  96.             get { return dateSeperator; }  
  97.             set  
  98.             {  
  99.                 if (value != '-' && value != '/' && value != '.')  
  100.                 {  
  101.                     dateSeperator = '-';  
  102.                 }  
  103.                 else  
  104.                 {  
  105.                     dateSeperator = value;  
  106.                 }  
  107.   
  108.                 textChars[4] = dateSeperator;  
  109.                 textChars[7] = dateSeperator;  
  110.   
  111.                 this.ShowDateText();  
  112.                 base.SelectionStart = 0;  
  113.             }  
  114.         }  
  115.   
  116.         public bool IsNull  
  117.         {  
  118.             get { return isNull; }  
  119.         }  
  120.   
  121.         public bool IsValid  
  122.         {  
  123.             get { return isValid; }  
  124.         }  
  125.   
  126.         public object Date  
  127.         {  
  128.             get  
  129.             {  
  130.                 if (isNull == true || isValid == false)  
  131.                 {  
  132.                     return null;  
  133.                 }  
  134.                 return date;  
  135.             }  
  136.             set  
  137.             {  
  138.                 if (value == null || value == DBNull.Value)  
  139.                 {  
  140.                     this.SetToNullDate();  
  141.                 }  
  142.                 else  
  143.                 {  
  144.                     date = (DateTime)value;  
  145.                     this.SetToGiveDate(date);  
  146.                 }  
  147.   
  148.                 if (base.ForeColor != validDateForeColor)  
  149.                 {  
  150.                     base.ForeColor = validDateForeColor;  
  151.                 }  
  152.                 this.Invalidate();  
  153.             }  
  154.         }  
  155.   
  156.         public void OnEditBoxDateChanged()  
  157.         {  
  158.             if (isValid == true || isNull == true)  
  159.             {  
  160.                 if (this.EditBoxDateChanged != null)  
  161.                 {  
  162.                     this.EditBoxDateChanged(this, EventArgs.Empty);  
  163.                 }  
  164.             }  
  165.         }  
  166.   
  167.         protected override void OnKeyDown(KeyEventArgs e)  
  168.         {  
  169.             if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && (e.KeyCode == Keys.Right) || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End)  
  170.             {  
  171.                 base.OnKeyDown(e);  
  172.                 return;  
  173.             }  
  174.   
  175.             if (e.KeyData == Keys.Tab || e.KeyData == Keys.Home || e.KeyData == Keys.End)  
  176.             {  
  177.                 base.OnKeyDown(e);  
  178.                 return;  
  179.             }  
  180.   
  181.             if (e.KeyCode == Keys.Back)  
  182.             {  
  183.                 this.KeyBackSpace();  
  184.             }  
  185.             else if (e.KeyCode == Keys.Delete)  
  186.             {  
  187.                 this.KeyDelete();  
  188.             }  
  189.             else if (e.KeyData == Keys.Left)  
  190.             {  
  191.                 this.MoveLeft();  
  192.             }  
  193.             else if (e.KeyData == Keys.Right)  
  194.             {  
  195.                 this.MoveRight();  
  196.             }  
  197.             else if ((e.KeyValue >= '0' && e.KeyValue <= '9') || e.KeyValue == ' ')  
  198.             {  
  199.                 this.KeyDigit(e.KeyValue);  
  200.             }  
  201.             else if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))  
  202.             {  
  203.                 int keyValue = (int)e.KeyCode - (int)Keys.NumPad0 + (int)Keys.D0;  
  204.                 this.KeyDigit(keyValue);  
  205.             }  
  206.   
  207.             e.SuppressKeyPress = true;  
  208.             e.Handled = true;  
  209.   
  210.             this.ParseDateText();  
  211.         }  
  212.   
  213.         protected override void OnLeave(EventArgs e)  
  214.         {  
  215.             this.NormalizeDateText();  
  216.             base.OnLeave(e);  
  217.         }  
  218.   
  219.         protected override void OnGotFocus(EventArgs e)  
  220.         {  
  221.             base.OnGotFocus(e);  
  222.             base.SelectionLength = 0;  
  223.         }  
  224.   
  225.         private void SetToNullDate()  
  226.         {  
  227.             textChars[0] = ' ';  
  228.             textChars[1] = ' ';  
  229.             textChars[2] = ' ';  
  230.             textChars[3] = ' ';  
  231.             textChars[4] = dateSeperator;  
  232.             textChars[5] = ' ';  
  233.             textChars[6] = ' ';  
  234.             textChars[7] = dateSeperator;  
  235.             textChars[8] = ' ';  
  236.             textChars[9] = ' ';  
  237.   
  238.             isNull = true;  
  239.             isValid = false;  
  240.   
  241.             this.ShowDateText();  
  242.             base.SelectionStart = 0;  
  243.         }  
  244.   
  245.         private void SetToGiveDate(DateTime date)  
  246.         {  
  247.             string today = date.ToString("yyyy-MM-dd");  
  248.             for (int k = 0; k < today.Length; k++)  
  249.             {  
  250.                 if (k != 4 && k != 7)  
  251.                 {  
  252.                     textChars[k] = today[k];  
  253.                 }  
  254.             }  
  255.   
  256.             isNull = false;  
  257.             isValid = true;  
  258.   
  259.             this.ShowDateText();  
  260.             base.SelectionStart = 0;  
  261.         }  
  262.   
  263.         private void SaveAsLastErrorText()  
  264.         {  
  265.             if (isNull == true)  
  266.             {  
  267.                 return;  
  268.             }  
  269.   
  270.             for (int k = 0; k < textChars.Length; k++)  
  271.             {  
  272.                 lastErrorTextChars[k] = textChars[k];  
  273.             }  
  274.         }  
  275.   
  276.         private void ParseDateText()  
  277.         {  
  278.             string y = new string(textChars, 0, 4);  
  279.             string m = new string(textChars, 5, 2);  
  280.             string d = new string(textChars, 8, 2);  
  281.   
  282.             string yy = y.Trim();  
  283.             string mm = m.Trim();  
  284.             string dd = d.Trim();  
  285.   
  286.             if (string.IsNullOrEmpty(yy) == true && string.IsNullOrEmpty(mm) == true && string.IsNullOrEmpty(dd) == true)  
  287.             {  
  288.                 bool preIsNull = isNull;  
  289.   
  290.                 isNull = true;  
  291.                 isValid = false;  
  292.   
  293.                 if (preIsNull != isNull)  
  294.                 {  
  295.                     this.ShowValidDateColor();  
  296.                     this.OnEditBoxDateChanged();  
  297.                 }  
  298.   
  299.                 return;  
  300.             }  
  301.   
  302.             isNull = false;  
  303.   
  304.             if (string.IsNullOrEmpty(yy) == true || string.IsNullOrEmpty(mm) == true || string.IsNullOrEmpty(dd) == true)  
  305.             {  
  306.                 isValid = false;  
  307.                 this.SaveAsLastErrorText();  
  308.                 this.ShowErrorDateColor();  
  309.                 return;  
  310.             }  
  311.   
  312.             if (Regex.IsMatch(yy, @"/d{4}") == false || Regex.IsMatch(mm, @"/d{1,2}") == false || Regex.IsMatch(dd, @"/d{1,2}") == false)  
  313.             {  
  314.                 isValid = false;  
  315.                 this.SaveAsLastErrorText();  
  316.                 this.ShowErrorDateColor();  
  317.                 return;  
  318.             }  
  319.   
  320.             int year = int.Parse(yy);  
  321.             int month = int.Parse(mm);  
  322.             int day = int.Parse(dd);  
  323.   
  324.             if (year < minDateYear || year > maxDateYear || month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month))  
  325.             {  
  326.                 isValid = false;  
  327.                 this.SaveAsLastErrorText();  
  328.                 this.ShowErrorDateColor();  
  329.                 return;  
  330.             }  
  331.   
  332.             isValid = true;  
  333.             this.ShowValidDateColor();  
  334.   
  335.             bool modified = false;  
  336.             if (year != date.Year || month != date.Month || day != date.Day)  
  337.             {  
  338.                 modified = true;  
  339.             }  
  340.   
  341.             date = new DateTime(year, month, day);  
  342.   
  343.             if (modified == true)  
  344.             {  
  345.                 this.OnEditBoxDateChanged();  
  346.             }  
  347.         }  
  348.   
  349.         private void NormalizeDateText()  
  350.         {  
  351.             if (isValid == false || isNull == true)  
  352.             {  
  353.                 return;  
  354.             }  
  355.   
  356.             if (textChars[5] == ' ' || textChars[6] == ' ' || textChars[8] == ' ' || textChars[9] == ' ')  
  357.             {  
  358.                 if (textChars[5] == ' ')  
  359.                 {  
  360.                     textChars[5] = '0';  
  361.                 }  
  362.   
  363.                 if (textChars[6] == ' ')  
  364.                 {  
  365.                     textChars[6] = textChars[5];  
  366.                     textChars[5] = '0';  
  367.                 }  
  368.   
  369.                 if (textChars[8] == ' ')  
  370.                 {  
  371.                     textChars[8] = '0';  
  372.                 }  
  373.   
  374.                 if (textChars[9] == ' ')  
  375.                 {  
  376.                     textChars[9] = textChars[8];  
  377.                     textChars[8] = '0';  
  378.                 }  
  379.   
  380.                 ShowDateText();  
  381.             }  
  382.         }  
  383.   
  384.         private void ShowDateText()  
  385.         {  
  386.             if (isNull == false && isValid == false)  
  387.             {  
  388.                 if (base.ForeColor != errorDateForeColor)  
  389.                 {  
  390.                     base.ForeColor = errorDateForeColor;  
  391.                 }  
  392.             }  
  393.             else  
  394.             {  
  395.                 if (base.ForeColor != validDateForeColor)  
  396.                 {  
  397.                     base.ForeColor = validDateForeColor;  
  398.                 }  
  399.             }  
  400.   
  401.             base.Text = new string(textChars);  
  402.         }  
  403.   
  404.         private void ShowValidDateColor()  
  405.         {  
  406.             if (base.ForeColor != validDateForeColor)  
  407.             {  
  408.                 base.ForeColor = validDateForeColor;  
  409.             }  
  410.             this.Invalidate();  
  411.         }  
  412.   
  413.         private void ShowErrorDateColor()  
  414.         {  
  415.             if (base.ForeColor != errorDateForeColor)  
  416.             {  
  417.                 base.ForeColor = errorDateForeColor;  
  418.             }  
  419.             this.Invalidate();  
  420.         }  
  421.   
  422.         public void ResumeLastErrorText()  
  423.         {  
  424.             for (int k = 0; k < textChars.Length; k++)  
  425.             {  
  426.                 textChars[k] = lastErrorTextChars[k];  
  427.             }  
  428.   
  429.             if (base.ForeColor != errorDateForeColor)  
  430.             {  
  431.                 base.ForeColor = errorDateForeColor;  
  432.             }  
  433.   
  434.             isValid = false;  
  435.             base.Text = new string(textChars);  
  436.             this.OnEditBoxDateChanged();  
  437.         }  
  438.   
  439.         private void KeyDelete()  
  440.         {  
  441.             if (base.SelectionLength <= 1)  
  442.             {  
  443.                 this.KeyDelete(base.SelectionStart);  
  444.             }  
  445.             else  
  446.             {  
  447.                 int start = base.SelectionStart + base.SelectionLength;  
  448.                 int end = base.SelectionStart + 1;  
  449.                 for (int k = start; k >= end; k--)  
  450.                 {  
  451.                     KeyBackSpace(k);  
  452.                 }  
  453.             }  
  454.         }  
  455.   
  456.         private void KeyDelete(int selectionStart)  
  457.         {  
  458.             if (AtTextEnd(selectionStart) == true)  
  459.             {  
  460.                 return;  
  461.             }  
  462.   
  463.             this.KeyBackSpace(selectionStart + 1);  
  464.         }  
  465.   
  466.         private void KeyBackSpace()  
  467.         {  
  468.             this.KeyBackSpace(base.SelectionStart);  
  469.         }  
  470.   
  471.         private void KeyBackSpace(int selectionStart)  
  472.         {  
  473.             int curPos = selectionStart;  
  474.   
  475.             if (curPos == 0)  
  476.             {  
  477.                 return;  
  478.             }  
  479.   
  480.             if (AtSeperatorRight(curPos) == true)  
  481.             {  
  482.                 base.SelectionStart = curPos - 1;  
  483.                 return;  
  484.             }  
  485.   
  486.             if (AtTextEnd(curPos) == true)  
  487.             {  
  488.                 textChars[textChars.Length - 1] = ' ';  
  489.                 ShowDateText();  
  490.                 base.SelectionStart = curPos - 1;  
  491.                 return;  
  492.             }  
  493.   
  494.             if (AtSeperatorLeft(curPos) == true)  
  495.             {  
  496.                 textChars[curPos - 1] = ' ';  
  497.                 ShowDateText();  
  498.                 base.SelectionStart = curPos - 1;  
  499.                 return;  
  500.             }  
  501.   
  502.             int k = curPos;  
  503.             while (AtSeperatorLeft(k) == false && AtTextEnd(k) == false)  
  504.             {  
  505.                 textChars[k - 1] = textChars[k];  
  506.                 k++;  
  507.             }  
  508.             textChars[k - 1] = ' ';  
  509.   
  510.             ShowDateText();  
  511.             base.SelectionStart = curPos - 1;  
  512.         }  
  513.   
  514.         private void KeyDigit(int keyValue)  
  515.         {  
  516.             if (AtTextEnd() == true)  
  517.             {  
  518.                 return;  
  519.             }  
  520.   
  521.             int curPos = base.SelectionStart;  
  522.             int newPos = curPos;  
  523.   
  524.             if (AtSeperatorLeft() == true)  
  525.             {  
  526.                 textChars[base.SelectionStart + 1] = (char)keyValue;  
  527.                 newPos = curPos + 2;  
  528.             }  
  529.             else if (AtSeperatorLeft(curPos + 1) == true)  
  530.             {  
  531.                 textChars[base.SelectionStart] = (char)keyValue;  
  532.                 newPos = curPos + 2;  
  533.             }  
  534.             else  
  535.             {  
  536.                 textChars[base.SelectionStart] = (char)keyValue;  
  537.   
  538.                 if (AtSeperatorRight(curPos + 1) == true)  
  539.                 {  
  540.                     curPos++;  
  541.                 }  
  542.                 newPos = curPos + 1;  
  543.             }  
  544.   
  545.             this.ShowDateText();  
  546.             base.SelectionStart = newPos;  
  547.         }  
  548.   
  549.         private void MoveLeft()  
  550.         {  
  551.             if (base.SelectionStart == 0)  
  552.             {  
  553.                 return;  
  554.             }  
  555.   
  556.             if (AtSeperatorRight(base.SelectionStart) == true)  
  557.             {  
  558.                 base.SelectionStart -= 2;  
  559.             }  
  560.             else  
  561.             {  
  562.                 base.SelectionStart -= 1;  
  563.             }  
  564.         }  
  565.   
  566.         private void MoveRight()  
  567.         {  
  568.             if (this.AtTextEnd() == true)  
  569.             {  
  570.                 return;  
  571.             }  
  572.   
  573.             if (AtSeperatorLeft(base.SelectionStart + 1) == true)  
  574.             {  
  575.                 base.SelectionStart += 2;  
  576.             }  
  577.             else  
  578.             {  
  579.                 base.SelectionStart += 1;  
  580.             }  
  581.         }  
  582.   
  583.         private bool AtSeperatorLeft(int curPos)  
  584.         {  
  585.             if (curPos == 4 || curPos == 7)  
  586.             {  
  587.                 return true;  
  588.             }  
  589.             return false;  
  590.         }  
  591.   
  592.         private bool AtSeperatorLeft()  
  593.         {  
  594.             return AtSeperatorLeft(base.SelectionStart);  
  595.         }  
  596.   
  597.         private bool AtSeperatorRight(int curPos)  
  598.         {  
  599.             if (curPos == 5 || curPos == 8)  
  600.             {  
  601.                 return true;  
  602.             }  
  603.             return false;  
  604.         }  
  605.   
  606.         private bool AtSeperatorRight()  
  607.         {  
  608.             return AtSeperatorRight(base.SelectionStart);  
  609.         }  
  610.   
  611.         private bool AtTextEnd(int curPos)  
  612.         {  
  613.             if (curPos == textChars.Length)  
  614.             {  
  615.                 return true;  
  616.             }  
  617.             return false;  
  618.         }  
  619.   
  620.         private bool AtTextEnd()  
  621.         {  
  622.             return AtTextEnd(base.SelectionStart);  
  623.         }  
  624.     }  
  625.   
  626.     [ToolboxItem(false)]  
  627.     public class TDatePicker : DateTimePicker  
  628.     {  
  629.         private bool isDropdown = false;  
  630.         private DateTime dateBeforeDropDown;  
  631.   
  632.         private const int WM_IME_SETCONTENT = 0x0281;  
  633.         private const int WM_CAPTURECHANGED = 0x0215;  
  634.         private const int WM_KEY_DOWN = 0x100;  
  635.         private const int WM_KEY_UP = 0x101;  
  636.         private const int WM_KEY_ENTER = 0x000d;  
  637.         private const int WM_KEY_ESC = 0x001b;  
  638.   
  639.         private bool msgHandledAfterCloseup = true;  
  640.         protected bool msgHandledByImeSetContent = false;  
  641.   
  642.         public event EventHandler DateConfirmed;  
  643.         public event EventHandler DateAbandoned;  
  644.   
  645.         protected override void OnDropDown(EventArgs eventargs)  
  646.         {  
  647.             msgHandledAfterCloseup = true;  
  648.             isDropdown = true;  
  649.   
  650.             dateBeforeDropDown = this.Value.Date;  
  651.             base.OnDropDown(eventargs);  
  652.         }  
  653.   
  654.         protected override void OnCloseUp(EventArgs eventargs)  
  655.         {  
  656.             isDropdown = false;  
  657.   
  658.             msgHandledAfterCloseup = false;  
  659.             msgHandledByImeSetContent = false;  
  660.   
  661.             base.OnCloseUp(eventargs);  
  662.         }  
  663.   
  664.         protected override void OnKeyDown(KeyEventArgs e)  
  665.         {  
  666.             if (isDropdown == true && (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right ||  
  667.                 e.KeyCode == Keys.Up || e.KeyCode == Keys.Down ||  
  668.                 e.KeyCode == Keys.Home || e.KeyCode == Keys.End ||  
  669.                 e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown))  
  670.             {  
  671.                 base.OnKeyDown(e);  // 不需要捕获 ESC/Enter 健   
  672.             }  
  673.             else  
  674.             {  
  675.                 e.SuppressKeyPress = true;  
  676.                 e.Handled = true;  
  677.             }  
  678.         }  
  679.   
  680.         private void OnDateConfirmed()  
  681.         {  
  682.             if (this.DateConfirmed != null)  
  683.             {  
  684.                 this.DateConfirmed(this, EventArgs.Empty);  
  685.             }  
  686.         }  
  687.   
  688.         private void OnDateAbandoned()  
  689.         {  
  690.             if (this.Value.Date != dateBeforeDropDown)  
  691.             {  
  692.                 this.Value = dateBeforeDropDown;  
  693.             }  
  694.   
  695.             if (this.DateConfirmed != null)  
  696.             {  
  697.                 this.DateAbandoned(this, EventArgs.Empty);  
  698.             }  
  699.         }  
  700.   
  701.         protected override void WndProc(ref Message m)  
  702.         {  
  703.             if (msgHandledAfterCloseup == false && m.Msg == WM_CAPTURECHANGED)  
  704.             {  
  705.                 msgHandledAfterCloseup = true;  
  706.                 msgHandledByImeSetContent = false;  
  707.                 this.OnDateAbandoned();  
  708.             }  
  709.             else if (msgHandledAfterCloseup == false && m.Msg == WM_KEY_UP && m.WParam.ToInt32() == WM_KEY_ENTER)  
  710.             {  
  711.                 msgHandledAfterCloseup = true;  
  712.                 msgHandledByImeSetContent = false;  
  713.                 this.OnDateConfirmed();  
  714.             }  
  715.             else if ((msgHandledAfterCloseup == false || msgHandledByImeSetContent == true) && (m.Msg == WM_KEY_UP && m.WParam.ToInt32() == WM_KEY_ESC))  
  716.             {  
  717.                 msgHandledAfterCloseup = true;  
  718.                 msgHandledByImeSetContent = false;  
  719.                 this.OnDateAbandoned();  
  720.             }  
  721.             else if (msgHandledAfterCloseup == false && m.Msg == WM_IME_SETCONTENT)  
  722.             {  
  723.                 msgHandledAfterCloseup = true;  
  724.                 msgHandledByImeSetContent = true;  
  725.                 this.OnDateConfirmed();  
  726.             }  
  727.   
  728.             if (m.Msg == WM_KEY_DOWN)  // keydown   
  729.             {  
  730.                 msgHandledByImeSetContent = false;  
  731.             }  
  732.   
  733.             base.WndProc(ref m);  
  734.         }  
  735.     }    
  736.   
  737.     public class TDateEditPicker : UserControl, INotifyPropertyChanged  
  738.     {  
  739.         private IContainer components = null;  
  740.   
  741.         private const int MINDATEYEAR = 1949;  
  742.         private const int MAXDATEYEAR = 2100;  
  743.   
  744.         private TDateEditBox dateEditBox;  
  745.         private TDatePicker datePicker;  
  746.   
  747.         private bool isNullWhenDropDown;  
  748.         private bool isValidWhenDropDown;  
  749.         private string version = "1.3";  
  750.         private int nativeEditBoxWidth;  
  751.   
  752.         public event PropertyChangedEventHandler PropertyChanged;  
  753.   
  754.         public TDateEditPicker()  
  755.         {  
  756.             this.InitializeComponent();  
  757.   
  758.             this.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never;  // 强制默认方式   
  759.   
  760.             this.dateEditBox.Leave += new EventHandler(this.DateEditBox_Leave);  
  761.             this.datePicker.DropDown += new EventHandler(this.DateTimePicker_DropDown);  
  762.             this.dateEditBox.EditBoxDateChanged += new EventHandler(this.DateEditBox_DateChanged);  
  763.             this.datePicker.DateConfirmed += new EventHandler(this.DateTimePicker_DataConfirmed);  
  764.             this.datePicker.DateAbandoned += new EventHandler(this.DateTimePicker_DateAbandoned);  
  765.         }  
  766.   
  767.         private void InitializeComponent()  
  768.         {  
  769.             this.dateEditBox = new TDateEditBox();  
  770.             this.datePicker = new TDatePicker();  
  771.   
  772.             this.SuspendLayout();  
  773.   
  774.             this.datePicker.Location = new Point(1, 0);  
  775.             this.datePicker.Name = "datePicker";  
  776.             this.datePicker.Value = DateTime.Now;  
  777.             this.datePicker.MinDate = new DateTime(this.dateEditBox.MinDateYear, 1, 1);  
  778.             this.datePicker.MaxDate = new DateTime(this.dateEditBox.MaxDateYear, 12, 31);  
  779.             this.datePicker.Format = DateTimePickerFormat.Custom;  
  780.             this.datePicker.CustomFormat = "          ";  
  781.   
  782.             this.dateEditBox.Location = new Point(0, 0);  
  783.             this.dateEditBox.Name = "dateEditBox";  
  784.             this.nativeEditBoxWidth = this.dateEditBox.Height;  
  785.   
  786.             this.Controls.Add(this.datePicker);  
  787.             this.Controls.Add(this.dateEditBox);  
  788.             this.Name = "TDateEditPicker";  
  789.             this.Size = new Size(this.datePicker.Width + 2, this.dateEditBox.Height + 2);  
  790.   
  791.             this.dateEditBox.BringToFront();  
  792.   
  793.             this.ResumeLayout();  
  794.         }  
  795.   
  796.         protected override void Dispose(bool disposing)  
  797.         {  
  798.             if (disposing == true)  
  799.             {  
  800.                 if (components != null)  
  801.                 {  
  802.                     components.Dispose();  
  803.                 }  
  804.             }  
  805.             base.Dispose(disposing);  
  806.         }  
  807.   
  808.         protected override void OnResize(EventArgs e)  
  809.         {  
  810.             base.OnResize(e);  
  811.               
  812.             int adjustWidth = 2;  
  813.             if (this.BorderStyle == BorderStyle.Fixed3D)  
  814.             {  
  815.                 adjustWidth = 3;  
  816.             }  
  817.   
  818.             this.datePicker.Width = this.Width - adjustWidth;  
  819.             this.dateEditBox.Width = this.datePicker.Width - this.nativeEditBoxWidth + 2;  
  820.         }  
  821.   
  822.         private void DateEditBox_Leave(object sender, EventArgs e)  
  823.         {  
  824.             if (this.IsValid == true && this.IsNull == false)  
  825.             {  
  826.                 this.datePicker.Value = (DateTime)dateEditBox.Date;  
  827.             }  
  828.             else  
  829.             {  
  830.                 this.datePicker.Value = DateTime.Now.Date;  
  831.             }  
  832.         }  
  833.   
  834.         private void DateEditBox_DateChanged(object sender, EventArgs e)  
  835.         {  
  836.             if (this.IsNull == true || this.IsValid == true)  
  837.             {  
  838.                 this.NotifyPropertyChanged("Date");  
  839.             }  
  840.         }  
  841.   
  842.         private void DateTimePicker_DropDown(object sender, EventArgs e)  
  843.         {  
  844.             isNullWhenDropDown = this.IsNull;  
  845.             isValidWhenDropDown = this.IsValid;  
  846.         }  
  847.   
  848.         private void DateTimePicker_DataConfirmed(object sender, EventArgs e)  
  849.         {  
  850.             this.dateEditBox.Date = this.datePicker.Value.Date;  
  851.             this.NotifyPropertyChanged("Date");  
  852.         }  
  853.   
  854.         private void DateTimePicker_DateAbandoned(object sender, EventArgs e)  
  855.         {  
  856.             if (isNullWhenDropDown == true)  
  857.             {  
  858.                 this.dateEditBox.Date = null;  
  859.             }  
  860.             else if (isValidWhenDropDown == false)  
  861.             {  
  862.                 this.dateEditBox.ResumeLastErrorText();  
  863.             }  
  864.         }  
  865.   
  866.         private void NotifyPropertyChanged(string info)  
  867.         {  
  868.             if (this.DataBindings != null && this.DataBindings.Count > 0)  
  869.             {  
  870.                 if (this.DataBindings[0].DataSourceUpdateMode == DataSourceUpdateMode.OnValidation)  // == never   
  871.                 {  
  872.                     return;  
  873.                 }  
  874.             }  
  875.   
  876.             if (this.PropertyChanged != null)  
  877.             {  
  878.                 this.PropertyChanged(thisnew PropertyChangedEventArgs(info));  
  879.             }  
  880.         }  
  881.   
  882.         [Category("Custom")]  
  883.         public string Version  
  884.         {  
  885.             get { return version; }  
  886.         }  
  887.   
  888.         [Category("Custom"), DefaultValue('-')]  
  889.         [Description("Set date seperator, only -./ threes.")]  
  890.         public char DateSeperator  
  891.         {  
  892.             get { return dateEditBox.DateSeperator; }  
  893.             set { dateEditBox.DateSeperator = value; }  
  894.         }  
  895.   
  896.         [Category("Custom"), DefaultValue(1950)]  
  897.         [Description("Set the min valid date year.")]  
  898.         public int MinDateYear  
  899.         {  
  900.             get { return this.dateEditBox.MinDateYear; }  
  901.             set  
  902.             {  
  903.                 int tmpValue = value;  
  904.                 if (tmpValue < MINDATEYEAR || tmpValue > MAXDATEYEAR)  
  905.                 {  
  906.                     tmpValue = MINDATEYEAR;  
  907.                 }  
  908.                 if (this.MinDateYear != tmpValue)  
  909.                 {  
  910.                     this.dateEditBox.MinDateYear = tmpValue;  
  911.                     this.datePicker.MinDate = new DateTime(tmpValue, 1, 1);  
  912.                 }  
  913.             }  
  914.         }  
  915.   
  916.         [Category("Custom"),DefaultValue(2060)]  
  917.         [Description("Set the max valid date year.")]  
  918.         public int MaxDateYear  
  919.         {  
  920.             get { return this.dateEditBox.MaxDateYear; }  
  921.             set  
  922.             {  
  923.                 int tmpValue = value;  
  924.                 if (tmpValue < MINDATEYEAR || tmpValue > MAXDATEYEAR)  
  925.                 {  
  926.                     tmpValue = MAXDATEYEAR;  
  927.                 }  
  928.                 else  
  929.                 {  
  930.                     this.dateEditBox.MaxDateYear = tmpValue;  
  931.                     this.datePicker.MaxDate = new DateTime(tmpValue, 12, 31);  
  932.                 }  
  933.             }  
  934.         }  
  935.   
  936.         [Category("Custom")]  
  937.         [Description("Is the date is null.")]  
  938.         public bool IsNull  
  939.         {  
  940.             get { return dateEditBox.IsNull; }  
  941.         }  
  942.   
  943.         [Category("Custom")]  
  944.         [Description("Is the date is valid.")]  
  945.         public bool IsValid  
  946.         {  
  947.             get { return dateEditBox.IsValid; }  
  948.         }  
  949.   
  950.         [Bindable(true), Browsable(false)]  
  951.         public Object Date  
  952.         {  
  953.             get  
  954.             {  
  955.                 return this.dateEditBox.Date;  
  956.             }  
  957.             set  
  958.             {  
  959.                 this.dateEditBox.Date = value;  
  960.                 this.NotifyPropertyChanged("Date");  
  961.             }  
  962.         }  
  963.   
  964.         [Category("Custom"), DefaultValue(typeof(Color), "Red")]  
  965.         [Description("Set the error date ForeColor.")]  
  966.         public Color DateErrorForeColor  
  967.         {  
  968.             get { return this.dateEditBox.ErrorDateForeColor; }  
  969.             set { this.dateEditBox.ErrorDateForeColor = value; }  
  970.         }  
  971.   
  972.         [Category("Custom"), DefaultValue(true)]  
  973.         [Description("Is the date picker enabled.")]  
  974.         public bool DatePickerEnabled  
  975.         {  
  976.             get { return datePicker.Enabled; }  
  977.             set { datePicker.Enabled = value; }  
  978.         }  
  979.   
  980.         [Category("Custom"), DefaultValue(true)]  
  981.         [Description("Is the date picker visible.")]  
  982.         public bool DatePickerVisible  
  983.         {  
  984.             get { return datePicker.Visible; }  
  985.             set { datePicker.Visible = value; }  
  986.         }  
  987.   
  988.         [Category("Custom")]  
  989.         public Font CalendarFont  
  990.         {  
  991.             get { return datePicker.CalendarFont; }  
  992.             set { datePicker.CalendarFont = value; }  
  993.         }  
  994.   
  995.         [Category("Custom")]  
  996.         [DefaultValue(typeof(Color), "ControlText")]  
  997.         public Color CalendarForeColor  
  998.         {  
  999.             get { return datePicker.CalendarForeColor; }  
  1000.             set { datePicker.CalendarForeColor = value; }  
  1001.         }  
  1002.   
  1003.         [Category("Custom")]  
  1004.         [DefaultValue(typeof(Color), "Window")]  
  1005.         public Color CalendarMonthBackColor  
  1006.         {  
  1007.             get { return datePicker.CalendarMonthBackground; }  
  1008.             set { datePicker.CalendarMonthBackground = value; }  
  1009.         }  
  1010.   
  1011.         [Category("Custom")]  
  1012.         [DefaultValue(typeof(Color), "ActiveCaption")]  
  1013.         public Color CalendarTitleForeColor  
  1014.         {  
  1015.             get { return datePicker.CalendarTitleForeColor; }  
  1016.             set { datePicker.CalendarTitleForeColor = value; }  
  1017.         }  
  1018.   
  1019.         [Category("Custom")]  
  1020.         [DefaultValue(typeof(Color), "ActiveCaptionText")]  
  1021.         public Color CalendarTitleBackColor  
  1022.         {  
  1023.             get { return datePicker.CalendarTitleBackColor; }  
  1024.             set { datePicker.CalendarTitleBackColor = value; }  
  1025.         }  
  1026.   
  1027.         [Category("Custom")]  
  1028.         [DefaultValue(typeof(Color), "GrayText")]  
  1029.         public Color CalendarTrailingForeColor  
  1030.         {  
  1031.             get { return datePicker.CalendarTrailingForeColor; }  
  1032.             set { datePicker.CalendarTrailingForeColor = value; }  
  1033.         }  
  1034.   
  1035.   
  1036.         [Category("Custom")]  
  1037.         [DefaultValue(typeof(Color), "WindowText")]  
  1038.         [Description("Set the valid date ForeColor.")]  
  1039.         public Color DateTextForeColor  
  1040.         {  
  1041.             get { return dateEditBox.ForeColor; }  
  1042.             set { dateEditBox.ForeColor = value; }  
  1043.         }  
  1044.   
  1045.         [Browsable(false)]  
  1046.         public new Color ForeColor  
  1047.         {  
  1048.             get { return base.ForeColor; }  
  1049.             set { }  
  1050.         }  
  1051.   
  1052.         [Category("Custom")]  
  1053.         [DefaultValue(typeof(Color), "Window")]  
  1054.         [Description("Set the date edit box BackColor.")]  
  1055.         public Color DateBoxBackColor  
  1056.         {  
  1057.             get { return dateEditBox.BackColor; }  
  1058.             set { dateEditBox.BackColor = value; }  
  1059.         }  
  1060.   
  1061.         [Browsable(false)]  
  1062.         public new Color BackColor  
  1063.         {  
  1064.             get { return base.BackColor; }  
  1065.             set { }  
  1066.         }  
  1067.   
  1068.     }  
  1069.   
  1070. }  
using System; using System.Windows.Forms; using System.ComponentModel; using System.Drawing; using System.Text.RegularExpressions; namespace CSUST.Data { [ToolboxItem(false)] public class TDateEditBox : TextBox { private const int MaxTextLength = 10; // 固定10个字符 private char[] textChars = new char[] { ' ', ' ', ' ', ' ', '-', ' ', ' ', '-', ' ', ' ' }; private char[] lastErrorTextChars = new char[MaxTextLength]; private char dateSeperator = '-'; // 分隔 private int minDateYear = 1950; private int maxDateYear = 2060; private bool isNull = true; private bool isValid = false; private Color validDateForeColor; private Color errorDateForeColor = Color.Red; private DateTime date; public event EventHandler EditBoxDateChanged; public TDateEditBox() { base.MaxLength = MaxTextLength; this.ContextMenu = new ContextMenu(); validDateForeColor = base.ForeColor; this.SetToNullDate(); } public new int MaxLength { get { return base.MaxLength; } set { base.MaxLength = MaxTextLength; } } public int MinDateYear { get { return minDateYear; } set { minDateYear = value; } } public int MaxDateYear { get { return maxDateYear; } set { maxDateYear = value; } } public Color ErrorDateForeColor { get { return errorDateForeColor; } set { errorDateForeColor = value; if (isValid == false && isNull == false) { this.ShowErrorDateColor(); } } } public override Color ForeColor { get { return base.ForeColor; } set { if (value != errorDateForeColor) { base.ForeColor = value; validDateForeColor = value; } if (isNull == true || isValid == true) { this.ShowValidDateColor(); } } } public char DateSeperator { get { return dateSeperator; } set { if (value != '-' && value != '/' && value != '.') { dateSeperator = '-'; } else { dateSeperator = value; } textChars[4] = dateSeperator; textChars[7] = dateSeperator; this.ShowDateText(); base.SelectionStart = 0; } } public bool IsNull { get { return isNull; } } public bool IsValid { get { return isValid; } } public object Date { get { if (isNull == true || isValid == false) { return null; } return date; } set { if (value == null || value == DBNull.Value) { this.SetToNullDate(); } else { date = (DateTime)value; this.SetToGiveDate(date); } if (base.ForeColor != validDateForeColor) { base.ForeColor = validDateForeColor; } this.Invalidate(); } } public void OnEditBoxDateChanged() { if (isValid == true || isNull == true) { if (this.EditBoxDateChanged != null) { this.EditBoxDateChanged(this, EventArgs.Empty); } } } protected override void OnKeyDown(KeyEventArgs e) { if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && (e.KeyCode == Keys.Right) || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End) { base.OnKeyDown(e); return; } if (e.KeyData == Keys.Tab || e.KeyData == Keys.Home || e.KeyData == Keys.End) { base.OnKeyDown(e); return; } if (e.KeyCode == Keys.Back) { this.KeyBackSpace(); } else if (e.KeyCode == Keys.Delete) { this.KeyDelete(); } else if (e.KeyData == Keys.Left) { this.MoveLeft(); } else if (e.KeyData == Keys.Right) { this.MoveRight(); } else if ((e.KeyValue >= '0' && e.KeyValue <= '9') || e.KeyValue == ' ') { this.KeyDigit(e.KeyValue); } else if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) { int keyValue = (int)e.KeyCode - (int)Keys.NumPad0 + (int)Keys.D0; this.KeyDigit(keyValue); } e.SuppressKeyPress = true; e.Handled = true; this.ParseDateText(); } protected override void OnLeave(EventArgs e) { this.NormalizeDateText(); base.OnLeave(e); } protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); base.SelectionLength = 0; } private void SetToNullDate() { textChars[0] = ' '; textChars[1] = ' '; textChars[2] = ' '; textChars[3] = ' '; textChars[4] = dateSeperator; textChars[5] = ' '; textChars[6] = ' '; textChars[7] = dateSeperator; textChars[8] = ' '; textChars[9] = ' '; isNull = true; isValid = false; this.ShowDateText(); base.SelectionStart = 0; } private void SetToGiveDate(DateTime date) { string today = date.ToString("yyyy-MM-dd"); for (int k = 0; k < today.Length; k++) { if (k != 4 && k != 7) { textChars[k] = today[k]; } } isNull = false; isValid = true; this.ShowDateText(); base.SelectionStart = 0; } private void SaveAsLastErrorText() { if (isNull == true) { return; } for (int k = 0; k < textChars.Length; k++) { lastErrorTextChars[k] = textChars[k]; } } private void ParseDateText() { string y = new string(textChars, 0, 4); string m = new string(textChars, 5, 2); string d = new string(textChars, 8, 2); string yy = y.Trim(); string mm = m.Trim(); string dd = d.Trim(); if (string.IsNullOrEmpty(yy) == true && string.IsNullOrEmpty(mm) == true && string.IsNullOrEmpty(dd) == true) { bool preIsNull = isNull; isNull = true; isValid = false; if (preIsNull != isNull) { this.ShowValidDateColor(); this.OnEditBoxDateChanged(); } return; } isNull = false; if (string.IsNullOrEmpty(yy) == true || string.IsNullOrEmpty(mm) == true || string.IsNullOrEmpty(dd) == true) { isValid = false; this.SaveAsLastErrorText(); this.ShowErrorDateColor(); return; } if (Regex.IsMatch(yy, @"/d{4}") == false || Regex.IsMatch(mm, @"/d{1,2}") == false || Regex.IsMatch(dd, @"/d{1,2}") == false) { isValid = false; this.SaveAsLastErrorText(); this.ShowErrorDateColor(); return; } int year = int.Parse(yy); int month = int.Parse(mm); int day = int.Parse(dd); if (year < minDateYear || year > maxDateYear || month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month)) { isValid = false; this.SaveAsLastErrorText(); this.ShowErrorDateColor(); return; } isValid = true; this.ShowValidDateColor(); bool modified = false; if (year != date.Year || month != date.Month || day != date.Day) { modified = true; } date = new DateTime(year, month, day); if (modified == true) { this.OnEditBoxDateChanged(); } } private void NormalizeDateText() { if (isValid == false || isNull == true) { return; } if (textChars[5] == ' ' || textChars[6] == ' ' || textChars[8] == ' ' || textChars[9] == ' ') { if (textChars[5] == ' ') { textChars[5] = '0'; } if (textChars[6] == ' ') { textChars[6] = textChars[5]; textChars[5] = '0'; } if (textChars[8] == ' ') { textChars[8] = '0'; } if (textChars[9] == ' ') { textChars[9] = textChars[8]; textChars[8] = '0'; } ShowDateText(); } } private void ShowDateText() { if (isNull == false && isValid == false) { if (base.ForeColor != errorDateForeColor) { base.ForeColor = errorDateForeColor; } } else { if (base.ForeColor != validDateForeColor) { base.ForeColor = validDateForeColor; } } base.Text = new string(textChars); } private void ShowValidDateColor() { if (base.ForeColor != validDateForeColor) { base.ForeColor = validDateForeColor; } this.Invalidate(); } private void ShowErrorDateColor() { if (base.ForeColor != errorDateForeColor) { base.ForeColor = errorDateForeColor; } this.Invalidate(); } public void ResumeLastErrorText() { for (int k = 0; k < textChars.Length; k++) { textChars[k] = lastErrorTextChars[k]; } if (base.ForeColor != errorDateForeColor) { base.ForeColor = errorDateForeColor; } isValid = false; base.Text = new string(textChars); this.OnEditBoxDateChanged(); } private void KeyDelete() { if (base.SelectionLength <= 1) { this.KeyDelete(base.SelectionStart); } else { int start = base.SelectionStart + base.SelectionLength; int end = base.SelectionStart + 1; for (int k = start; k >= end; k--) { KeyBackSpace(k); } } } private void KeyDelete(int selectionStart) { if (AtTextEnd(selectionStart) == true) { return; } this.KeyBackSpace(selectionStart + 1); } private void KeyBackSpace() { this.KeyBackSpace(base.SelectionStart); } private void KeyBackSpace(int selectionStart) { int curPos = selectionStart; if (curPos == 0) { return; } if (AtSeperatorRight(curPos) == true) { base.SelectionStart = curPos - 1; return; } if (AtTextEnd(curPos) == true) { textChars[textChars.Length - 1] = ' '; ShowDateText(); base.SelectionStart = curPos - 1; return; } if (AtSeperatorLeft(curPos) == true) { textChars[curPos - 1] = ' '; ShowDateText(); base.SelectionStart = curPos - 1; return; } int k = curPos; while (AtSeperatorLeft(k) == false && AtTextEnd(k) == false) { textChars[k - 1] = textChars[k]; k++; } textChars[k - 1] = ' '; ShowDateText(); base.SelectionStart = curPos - 1; } private void KeyDigit(int keyValue) { if (AtTextEnd() == true) { return; } int curPos = base.SelectionStart; int newPos = curPos; if (AtSeperatorLeft() == true) { textChars[base.SelectionStart + 1] = (char)keyValue; newPos = curPos + 2; } else if (AtSeperatorLeft(curPos + 1) == true) { textChars[base.SelectionStart] = (char)keyValue; newPos = curPos + 2; } else { textChars[base.SelectionStart] = (char)keyValue; if (AtSeperatorRight(curPos + 1) == true) { curPos++; } newPos = curPos + 1; } this.ShowDateText(); base.SelectionStart = newPos; } private void MoveLeft() { if (base.SelectionStart == 0) { return; } if (AtSeperatorRight(base.SelectionStart) == true) { base.SelectionStart -= 2; } else { base.SelectionStart -= 1; } } private void MoveRight() { if (this.AtTextEnd() == true) { return; } if (AtSeperatorLeft(base.SelectionStart + 1) == true) { base.SelectionStart += 2; } else { base.SelectionStart += 1; } } private bool AtSeperatorLeft(int curPos) { if (curPos == 4 || curPos == 7) { return true; } return false; } private bool AtSeperatorLeft() { return AtSeperatorLeft(base.SelectionStart); } private bool AtSeperatorRight(int curPos) { if (curPos == 5 || curPos == 8) { return true; } return false; } private bool AtSeperatorRight() { return AtSeperatorRight(base.SelectionStart); } private bool AtTextEnd(int curPos) { if (curPos == textChars.Length) { return true; } return false; } private bool AtTextEnd() { return AtTextEnd(base.SelectionStart); } } [ToolboxItem(false)] public class TDatePicker : DateTimePicker { private bool isDropdown = false; private DateTime dateBeforeDropDown; private const int WM_IME_SETCONTENT = 0x0281; private const int WM_CAPTURECHANGED = 0x0215; private const int WM_KEY_DOWN = 0x100; private const int WM_KEY_UP = 0x101; private const int WM_KEY_ENTER = 0x000d; private const int WM_KEY_ESC = 0x001b; private bool msgHandledAfterCloseup = true; protected bool msgHandledByImeSetContent = false; public event EventHandler DateConfirmed; public event EventHandler DateAbandoned; protected override void OnDropDown(EventArgs eventargs) { msgHandledAfterCloseup = true; isDropdown = true; dateBeforeDropDown = this.Value.Date; base.OnDropDown(eventargs); } protected override void OnCloseUp(EventArgs eventargs) { isDropdown = false; msgHandledAfterCloseup = false; msgHandledByImeSetContent = false; base.OnCloseUp(eventargs); } protected override void OnKeyDown(KeyEventArgs e) { if (isDropdown == true && (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown)) { base.OnKeyDown(e); // 不需要捕获 ESC/Enter 健 } else { e.SuppressKeyPress = true; e.Handled = true; } } private void OnDateConfirmed() { if (this.DateConfirmed != null) { this.DateConfirmed(this, EventArgs.Empty); } } private void OnDateAbandoned() { if (this.Value.Date != dateBeforeDropDown) { this.Value = dateBeforeDropDown; } if (this.DateConfirmed != null) { this.DateAbandoned(this, EventArgs.Empty); } } protected override void WndProc(ref Message m) { if (msgHandledAfterCloseup == false && m.Msg == WM_CAPTURECHANGED) { msgHandledAfterCloseup = true; msgHandledByImeSetContent = false; this.OnDateAbandoned(); } else if (msgHandledAfterCloseup == false && m.Msg == WM_KEY_UP && m.WParam.ToInt32() == WM_KEY_ENTER) { msgHandledAfterCloseup = true; msgHandledByImeSetContent = false; this.OnDateConfirmed(); } else if ((msgHandledAfterCloseup == false || msgHandledByImeSetContent == true) && (m.Msg == WM_KEY_UP && m.WParam.ToInt32() == WM_KEY_ESC)) { msgHandledAfterCloseup = true; msgHandledByImeSetContent = false; this.OnDateAbandoned(); } else if (msgHandledAfterCloseup == false && m.Msg == WM_IME_SETCONTENT) { msgHandledAfterCloseup = true; msgHandledByImeSetContent = true; this.OnDateConfirmed(); } if (m.Msg == WM_KEY_DOWN) // keydown { msgHandledByImeSetContent = false; } base.WndProc(ref m); } } public class TDateEditPicker : UserControl, INotifyPropertyChanged { private IContainer components = null; private const int MINDATEYEAR = 1949; private const int MAXDATEYEAR = 2100; private TDateEditBox dateEditBox; private TDatePicker datePicker; private bool isNullWhenDropDown; private bool isValidWhenDropDown; private string version = "1.3"; private int nativeEditBoxWidth; public event PropertyChangedEventHandler PropertyChanged; public TDateEditPicker() { this.InitializeComponent(); this.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never; // 强制默认方式 this.dateEditBox.Leave += new EventHandler(this.DateEditBox_Leave); this.datePicker.DropDown += new EventHandler(this.DateTimePicker_DropDown); this.dateEditBox.EditBoxDateChanged += new EventHandler(this.DateEditBox_DateChanged); this.datePicker.DateConfirmed += new EventHandler(this.DateTimePicker_DataConfirmed); this.datePicker.DateAbandoned += new EventHandler(this.DateTimePicker_DateAbandoned); } private void InitializeComponent() { this.dateEditBox = new TDateEditBox(); this.datePicker = new TDatePicker(); this.SuspendLayout(); this.datePicker.Location = new Point(1, 0); this.datePicker.Name = "datePicker"; this.datePicker.Value = DateTime.Now; this.datePicker.MinDate = new DateTime(this.dateEditBox.MinDateYear, 1, 1); this.datePicker.MaxDate = new DateTime(this.dateEditBox.MaxDateYear, 12, 31); this.datePicker.Format = DateTimePickerFormat.Custom; this.datePicker.CustomFormat = " "; this.dateEditBox.Location = new Point(0, 0); this.dateEditBox.Name = "dateEditBox"; this.nativeEditBoxWidth = this.dateEditBox.Height; this.Controls.Add(this.datePicker); this.Controls.Add(this.dateEditBox); this.Name = "TDateEditPicker"; this.Size = new Size(this.datePicker.Width + 2, this.dateEditBox.Height + 2); this.dateEditBox.BringToFront(); this.ResumeLayout(); } protected override void Dispose(bool disposing) { if (disposing == true) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } protected override void OnResize(EventArgs e) { base.OnResize(e); int adjustWidth = 2; if (this.BorderStyle == BorderStyle.Fixed3D) { adjustWidth = 3; } this.datePicker.Width = this.Width - adjustWidth; this.dateEditBox.Width = this.datePicker.Width - this.nativeEditBoxWidth + 2; } private void DateEditBox_Leave(object sender, EventArgs e) { if (this.IsValid == true && this.IsNull == false) { this.datePicker.Value = (DateTime)dateEditBox.Date; } else { this.datePicker.Value = DateTime.Now.Date; } } private void DateEditBox_DateChanged(object sender, EventArgs e) { if (this.IsNull == true || this.IsValid == true) { this.NotifyPropertyChanged("Date"); } } private void DateTimePicker_DropDown(object sender, EventArgs e) { isNullWhenDropDown = this.IsNull; isValidWhenDropDown = this.IsValid; } private void DateTimePicker_DataConfirmed(object sender, EventArgs e) { this.dateEditBox.Date = this.datePicker.Value.Date; this.NotifyPropertyChanged("Date"); } private void DateTimePicker_DateAbandoned(object sender, EventArgs e) { if (isNullWhenDropDown == true) { this.dateEditBox.Date = null; } else if (isValidWhenDropDown == false) { this.dateEditBox.ResumeLastErrorText(); } } private void NotifyPropertyChanged(string info) { if (this.DataBindings != null && this.DataBindings.Count > 0) { if (this.DataBindings[0].DataSourceUpdateMode == DataSourceUpdateMode.OnValidation) // == never { return; } } if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(info)); } } [Category("Custom")] public string Version { get { return version; } } [Category("Custom"), DefaultValue('-')] [Description("Set date seperator, only -./ threes.")] public char DateSeperator { get { return dateEditBox.DateSeperator; } set { dateEditBox.DateSeperator = value; } } [Category("Custom"), DefaultValue(1950)] [Description("Set the min valid date year.")] public int MinDateYear { get { return this.dateEditBox.MinDateYear; } set { int tmpValue = value; if (tmpValue < MINDATEYEAR || tmpValue > MAXDATEYEAR) { tmpValue = MINDATEYEAR; } if (this.MinDateYear != tmpValue) { this.dateEditBox.MinDateYear = tmpValue; this.datePicker.MinDate = new DateTime(tmpValue, 1, 1); } } } [Category("Custom"),DefaultValue(2060)] [Description("Set the max valid date year.")] public int MaxDateYear { get { return this.dateEditBox.MaxDateYear; } set { int tmpValue = value; if (tmpValue < MINDATEYEAR || tmpValue > MAXDATEYEAR) { tmpValue = MAXDATEYEAR; } else { this.dateEditBox.MaxDateYear = tmpValue; this.datePicker.MaxDate = new DateTime(tmpValue, 12, 31); } } } [Category("Custom")] [Description("Is the date is null.")] public bool IsNull { get { return dateEditBox.IsNull; } } [Category("Custom")] [Description("Is the date is valid.")] public bool IsValid { get { return dateEditBox.IsValid; } } [Bindable(true), Browsable(false)] public Object Date { get { return this.dateEditBox.Date; } set { this.dateEditBox.Date = value; this.NotifyPropertyChanged("Date"); } } [Category("Custom"), DefaultValue(typeof(Color), "Red")] [Description("Set the error date ForeColor.")] public Color DateErrorForeColor { get { return this.dateEditBox.ErrorDateForeColor; } set { this.dateEditBox.ErrorDateForeColor = value; } } [Category("Custom"), DefaultValue(true)] [Description("Is the date picker enabled.")] public bool DatePickerEnabled { get { return datePicker.Enabled; } set { datePicker.Enabled = value; } } [Category("Custom"), DefaultValue(true)] [Description("Is the date picker visible.")] public bool DatePickerVisible { get { return datePicker.Visible; } set { datePicker.Visible = value; } } [Category("Custom")] public Font CalendarFont { get { return datePicker.CalendarFont; } set { datePicker.CalendarFont = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "ControlText")] public Color CalendarForeColor { get { return datePicker.CalendarForeColor; } set { datePicker.CalendarForeColor = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "Window")] public Color CalendarMonthBackColor { get { return datePicker.CalendarMonthBackground; } set { datePicker.CalendarMonthBackground = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "ActiveCaption")] public Color CalendarTitleForeColor { get { return datePicker.CalendarTitleForeColor; } set { datePicker.CalendarTitleForeColor = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "ActiveCaptionText")] public Color CalendarTitleBackColor { get { return datePicker.CalendarTitleBackColor; } set { datePicker.CalendarTitleBackColor = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "GrayText")] public Color CalendarTrailingForeColor { get { return datePicker.CalendarTrailingForeColor; } set { datePicker.CalendarTrailingForeColor = value; } } [Category("Custom")] [DefaultValue(typeof(Color), "WindowText")] [Description("Set the valid date ForeColor.")] public Color DateTextForeColor { get { return dateEditBox.ForeColor; } set { dateEditBox.ForeColor = value; } } [Browsable(false)] public new Color ForeColor { get { return base.ForeColor; } set { } } [Category("Custom")] [DefaultValue(typeof(Color), "Window")] [Description("Set the date edit box BackColor.")] public Color DateBoxBackColor { get { return dateEditBox.BackColor; } set { dateEditBox.BackColor = value; } } [Browsable(false)] public new Color BackColor { get { return base.BackColor; } set { } } } }

 

你可能感兴趣的:(Date,String,dropdown)