繁体   English   中英

Datetimepicker文本值更改,但值未提供期望值

[英]Datetimepicker text value changes but value doesn't give the expected value

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

Source1: 覆盖DateTimePicker以添加周数时设置日历大小

Source 2: Win 7 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事件添加方法,我已经发现该事件永远不会发生。 我在网上发现了一些类似的问题,但都可以通过将datetimepicker的checked属性设置为true来解决。 就我而言, checked属性已经为true。

我怎么做才能打破datetimepicker?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM