Datetimepicker文本值更改但值未提供预期值

时间:2015-10-01 13:09:50

标签: c# .net winforms datetimepicker

我创建了一个类更改了calander的外观。 该类基于以前的stackoverflow问题:

Source1:Setting calendar size when overriding DateTimePicker to add week numbers

Source2:Increase Font Size of DateTimePicker Calender in Win7 Aero Theme

这是班级:

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事件添加一个方法来发现这个事件永远不会发生。我在网上发现了一些类似的问题,但这些都是通过将datetimepicker的checked属性设置为true来解决的。在我的情况下,checked属性已经为真。

我怎么做才能打破datetimepicker?

1 个答案:

答案 0 :(得分:0)

我必须手动创建日期时间选择器,而不是将其从工具箱拖到设计器中。

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);