fork download
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace CSP.Views.CustomControls
  5. {
  6. public class NullableDateTimePicker : DateTimePicker
  7. {
  8. private DateTime? SavedValue { get; set; }
  9. private DateTimePickerFormat SavedFormat { get; set; }
  10. private string SavedCustomFormat { get; set; }
  11. private bool IsNullValue { get; set; }
  12.  
  13. public NullableDateTimePicker()
  14. {
  15. Format = DateTimePickerFormat.Custom;
  16. CustomFormat = "yyyy年MM月dd日";
  17. Value = DateTime.Now;
  18. Text = Value.ToString();
  19. IsNullValue = false;
  20. }
  21.  
  22. public NullableDateTimePicker(DateTime dateTime)
  23. {
  24. Format = DateTimePickerFormat.Custom;
  25. CustomFormat = "yyyy年MM月dd日";
  26. Value = dateTime;
  27. Text = Value.ToString();
  28. IsNullValue = false;
  29. }
  30.  
  31. public new DateTime? Value
  32. {
  33. get;
  34. set;
  35. }
  36.  
  37. protected override void OnKeyDown(KeyEventArgs e)
  38. {
  39. base.OnKeyDown(e);
  40.  
  41. if (e.KeyCode == Keys.Delete)
  42. {
  43. if (!IsNullValue)
  44. {
  45. SavedFormat = Format;
  46. SavedCustomFormat = CustomFormat;
  47. SavedValue = Value;
  48.  
  49. Value = null;
  50. CustomFormat = " ";
  51.  
  52. IsNullValue = true;
  53. }
  54. }
  55. else
  56. {
  57. if (IsNullValue)
  58. {
  59. Format = SavedFormat;
  60. CustomFormat = SavedCustomFormat;
  61. Value = SavedValue;
  62.  
  63. IsNullValue = false;
  64. }
  65. }
  66. }
  67.  
  68. protected override void OnTextChanged(EventArgs e)
  69. {
  70. if (String.IsNullOrWhiteSpace(Text))
  71. {
  72. Value = null;
  73. IsNullValue = true;
  74. }
  75. else
  76. {
  77. Value = DateTime.Parse(Text);
  78. IsNullValue = false;
  79. }
  80. }
  81.  
  82. protected override void OnValueChanged(EventArgs e)
  83. {
  84. if (IsNullValue)
  85. {
  86. Format = SavedFormat;
  87. CustomFormat = SavedCustomFormat;
  88.  
  89. IsNullValue = false;
  90. }
  91.  
  92. if (!String.IsNullOrWhiteSpace(Text))
  93. Value = DateTime.Parse(Text);
  94. else
  95. Value = null;
  96. }
  97. }
  98. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(2,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
prog.cs(6,43): error CS0246: The type or namespace name `DateTimePicker' could not be found. Are you missing an assembly reference?
prog.cs(9,17): error CS0246: The type or namespace name `DateTimePickerFormat' could not be found. Are you missing an assembly reference?
prog.cs(31,30): warning CS0109: The member `CSP.Views.CustomControls.NullableDateTimePicker.Value' does not hide an inherited member. The new keyword is not required
prog.cs(37,43): error CS0246: The type or namespace name `KeyEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(68,33): error CS0115: `CSP.Views.CustomControls.NullableDateTimePicker.OnTextChanged(System.EventArgs)' is marked as an override but no suitable method found to override
prog.cs(82,33): error CS0115: `CSP.Views.CustomControls.NullableDateTimePicker.OnValueChanged(System.EventArgs)' is marked as an override but no suitable method found to override
Compilation failed: 6 error(s), 1 warnings
stdout
Standard output is empty