日期时间选择器文本值更改,但值没有给出期望值
本文关键字:期望值 选择器 时间 文本 日期 | 更新日期: 2023-09-27 18:15:34
我已经创建了一个更改日历外观的类。这个类是基于这些以前的stackoverflow问题:
Source1:在重写DateTimePicker以添加周数时设置日历大小
Source2:增加Win7 Aero主题中DateTimePicker日历的字体大小
这是类:
class DateTimePickerImpl : DateTimePicker
{
private const int McmFirst = 0x1000;
private const int McmGetminreqrect = (McmFirst + 9);
private const int McsWeeknumbers = 0x4;
private const int DtmFirst = 0x1000;
private const int DtmGetmonthcal = (DtmFirst + 8);
[DllImport("User32.dll")]
private static extern int GetWindowLong(IntPtr handleToWindow, int offsetToValueToGet);
[DllImport("User32.dll")]
private static extern int SetWindowLong(IntPtr h, int index, int value);
[DllImport("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
[DllImport("User32.dll")]
private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);
[DllImport("User32.dll")]
private static extern IntPtr GetParent(IntPtr h);
[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);
protected override void OnDropDown(EventArgs e)
{
CalendarFont = new Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
const int offsetToGetWindowsStyles = (-16);
IntPtr pointerToCalenderWindow = SendMessage(Handle, DtmGetmonthcal, 0, 0);
SetWindowTheme(pointerToCalenderWindow, "", "");
int styleForWindow = GetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles);
styleForWindow = styleForWindow | McsWeeknumbers;
Rectangle rect = new Rectangle();
SendMessage(pointerToCalenderWindow, McmGetminreqrect, 0, ref rect);
rect.Width = rect.Width + 30;
rect.Height = rect.Height + 10;
SetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles, styleForWindow);
MoveWindow(pointerToCalenderWindow, 0, 0, rect.Width, rect.Height, true);
IntPtr parentWindow = GetParent(pointerToCalenderWindow);
MoveWindow(parentWindow, 0, 0, rect.Width, rect.Height, true);
base.OnDropDown(e);
}
}
实现这个类之后,datetimepicker的可见文本变为您选择的任何内容。但是当使用dateTimePickerImpl.Value
时,它总是返回它开始的时间(datetimpicker加载的时间)。通过向ValueChanged
事件添加一个方法,我已经发现该事件永远不会发生。我在网上发现了一些类似的问题,但这些都是通过将日期时间选择器的checked
属性设置为true来解决的。在我的情况下,checked
属性已经为真。
我做了什么来破坏日期时间选择器?
我必须手动创建日期时间选择器,而不是将它从工具箱拖到设计器中。
DateTimePickerImpl dtp = new DateTimePickerImpl();
dtp.Location = new Point(3, 254);
dtp.Name = "dtp";
dtp.Size = new Size(271, 26);
dtp.TabIndex = 25;
panel1.Controls.Add(dtp);