DateTimePicker.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Globalization;
  2. namespace System.Windows.Forms
  3. {
  4. public class DateTimePicker : Control
  5. {
  6. protected new CustomControls.DateTimePicker control { get; }
  7. private DateTimePickerFormat format;
  8. public DateTimePickerFormat Format
  9. {
  10. get => format;
  11. set
  12. {
  13. format = value;
  14. if (format == DateTimePickerFormat.Short)
  15. CustomFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
  16. else if (format == DateTimePickerFormat.Long)
  17. CustomFormat = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
  18. else if (format == DateTimePickerFormat.Time)
  19. CustomFormat = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
  20. }
  21. }
  22. public string CustomFormat
  23. {
  24. get => format == DateTimePickerFormat.Custom ? control.CustomFormat : "";
  25. set => control.CustomFormat = value;
  26. }
  27. public DateTime Value
  28. {
  29. get => control.Value ?? DateTime.MinValue;
  30. set => control.Value = value;
  31. }
  32. public DateTime MinDate
  33. {
  34. get => control.DisplayDateStart ?? DateTime.MinValue;
  35. set => control.DisplayDateStart = value;
  36. }
  37. public DateTime MaxDate
  38. {
  39. get => control?.DisplayDateEnd ?? DateTime.MaxValue;
  40. set => control.DisplayDateEnd = value;
  41. }
  42. public bool ShowCheckBox { get; set; }
  43. public bool ShowUpDown { get; set; }
  44. public LeftRightAlignment DropDownAlign { get; set; }
  45. public bool Checked { get; set; }
  46. public event EventHandler ValueChanged;
  47. protected override void SetControlHeight(int value)
  48. {
  49. // ignore height
  50. }
  51. public DateTimePicker()
  52. {
  53. control = new();
  54. SetControl(control);
  55. control.SelectedDateChanged += (sender,e) => ValueChanged?.Invoke(this, EventArgs.Empty);
  56. BackColor = System.Drawing.SystemColors.Window;
  57. Width = 200;
  58. Format = DateTimePickerFormat.Long;
  59. }
  60. }
  61. }