DateTimePicker 的 MonthCalendar 的日期未更新

Posted

技术标签:

【中文标题】DateTimePicker 的 MonthCalendar 的日期未更新【英文标题】:The Date of the MonthCalendar of a DateTimePicker is not updated 【发布时间】:2021-06-10 17:49:57 【问题描述】:

在启动时,我将 DateTimePicker 值设置为最小日期值。 当用户使用MouseDown 事件单击 DateTimePicker 时,日期将更改为当前日期。 一个TextBox变成了当前日期,但是弹出的Calendar没有更新,仍然显示原来的Date。

Private Sub frm1_Load(sender As Object, e As EventArgs) Handles Me.Load        
        dtpReceiveDate.Value = clsCommon.NULL_DATE     
End Sub

 Private Sub dtpReceiveDate_MouseDown(sender As Object, e As MouseEventArgs) Handles dtpReceiveDate.MouseDown       
        dtpReceiveDate.Value = Today       
End Sub

【问题讨论】:

【参考方案1】:

MouseDown 事件在 DateTimePicker 的 MonthCalendar 控件更新为当前 DateTime 值后引发。 如果在打开 MonthCalendar 的 Button 上生成了 MouseDown 事件。 单击控件的日期部分不会打开 MonthCalendar。

我建议不要使用 MouseDown 事件来更新 DateTimePicker 的值,因为这将导致用户每次单击控件上的任何位置时重置日期。这可能会令人不安(这取决于您)。 您可以改用DropDown 事件:仅当用户单击向下箭头按钮时才会引发该事件。

▶ 您不能将ValueChanged 事件与此处显示的代码一起使用:这可能会导致死锁(鉴于所涉及的 Win32 控件的性质和 SendMessage 的使用,这种死锁类型可能会传播到整个系统,所以,不要)。

这里的DtpUpdateMonthCalendardDate() 方法向作为参数传递的DateTimePicker 发送DTM_GETMONTHCAL 消息。 如果成功获取了 MonthCalendar 的句柄,则它会向 MonthCalendar 控件发送MCM_SETCURSEL 消息,以将其日期与父 DateTimePicker 的日期对齐。 新的 Date 使用 SYSTEMTIME 结构传递,该结构使用 DateTimePicker 的值初始化。

Private Sub dtpReceiveDate_DropDown(sender As Object, e As EventArgs) Handles dtpReceiveDate.DropDown
    dtpReceiveDate.Value = Date.Today
    ' If result is False, something went wrong and the Date is not set
    Dim result = DtpUpdateMonthCalendardDate(dtpReceiveDate)
End Sub

Private Function DtpUpdateMonthCalendardDate(dtp As DateTimePicker) As Boolean
    If Not dtp.IsHandleCreated OrElse dtp.ShowUpDown Then Return False

    Dim hWndCal = SendMessage(dtp.Handle, DTM_GETMONTHCAL, 0, 0)
    If hWndCal = IntPtr.Zero Then Return False

    Dim sysTime = New SYSTEMTIME(dtp.Value)
    Return SendMessage(hWndCal, MCM_SETCURSEL, 0, sysTime) <> 0
End Function

Win32 声明

Friend Const DTM_FIRST As Integer = &H1000
Friend Const DTM_GETMONTHCAL As Integer = DTM_FIRST + 8

Friend Const MCM_FIRST As Integer = &H1000
Friend Const MCM_GETCURSEL As Integer = MCM_FIRST + 1
Friend Const MCM_SETCURSEL As Integer = MCM_FIRST + 2

<StructLayout(LayoutKind.Sequential)>
Friend Structure SYSTEMTIME
    Public Year As Short
    Public Month As Short
    Public DayOfWeek As Short
    Public Day As Short
    Public Hour As Short
    Public Minute As Short
    Public Second As Short
    Public Milliseconds As Short

    Public Sub New(dt As Date)
        Year = CShort(dt.Year)
        Month = CShort(dt.Month)
        DayOfWeek = CShort(dt.DayOfWeek)
        Day = CShort(dt.Day)
        Hour = CShort(dt.Hour)
        Minute = CShort(dt.Minute)
        Second = CShort(dt.Second)
        Milliseconds = CShort(dt.Millisecond)
    End Sub

    Public Function ToDateTime() As Date
        Return New DateTime(Year, Month, Day, Hour, Minute, Second, Milliseconds)
    End Function
End Structure

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Friend Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As IntPtr
End Function

<DllImport("User32", SetLastError:=True, CharSet:=CharSet.Auto)>
Friend Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, lParam As SYSTEMTIME) As Integer
End Function

【讨论】:

SendMessage 返回的 IntPtr 值检查中有错字:当然,它必须是 If hWndCal = IntPtr.Zero Then Return False

以上是关于DateTimePicker 的 MonthCalendar 的日期未更新的主要内容,如果未能解决你的问题,请参考以下文章

datetimepicker时间控件

DELPHI中怎么操作datetimepicker

mint ui datetimepicker 手机端jquery datetimepicker 总结应用

datetimepicker怎么选择公历

我正在寻找像 bootstrap datetimepicker 这样的materializecss datetimepicker [关闭]

DateTimePicker:选择日期和时间