none
如何让DateTimePicker以粗体显示特定日期? RRS feed

答案

  • MonthCalendar能够以粗体显示特定日期,DateTimePicker如何实现?


    韩立学

    DateTimePicker 不支持MonthCalendar 设置粗体时所使用BoldedDates属性。所以你得自己重写这个类来拓展来实现这个效果。

    remember make the reply as answer and vote the reply as helpful if it helps.

    Hi,

    正如Risa所说,DateTimePicker没有BoldedDates属性, 我们可以自己拓展DateTimePicker类来实现这样的功能。

    Imports System.ComponentModel
    
    Public Class SpecialDatePicker : Inherits DateTimePicker
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    
            Const WM_LBUTTONDOWN& = &H201
            ' Detect a left-button click on the control.
            If m.Msg = WM_LBUTTONDOWN Then
                ' Show the internal month calendar.
                Me.DropDownCalendar()
            Else
                MyBase.WndProc(m)
            End If
    
        End Sub
    
        Private my_calendar As New MonthCalendar With {
            .MaxSelectionCount = 1}        'limit it in single seletet mode
    
    
        <Browsable(False)> _
        Public ReadOnly Property MyCalendar() As MonthCalendar
            Get
                Return my_calendar
            End Get
        End Property  'Calendar
    
    
        Public Property BoldDates() As Date()
            Get
                Return Me.MyCalendar.BoldedDates
            End Get
            Set(ByVal Value As Date())
                Me.MyCalendar.BoldedDates = Value
            End Set
        End Property  'BoldDates
    
    
        Private Sub DateSelectedHandler(ByVal sender As Object, ByVal e As DateRangeEventArgs)
            Me.Value = Me.myCalendar.SelectionStart.ToShortDateString
            Me.myCalendar.Visible = False
        End Sub  'DateSelectedHandler
    
    
        Private Sub LostFocusHandler(ByVal sender As Object, ByVal e As EventArgs)
    
            Me.myCalendar.Visible = False
    
            MyBase.OnLeave(e)
    
        End Sub  'LostFocusHandler
    
    
        Private Sub DropDownCalendar()
            'Return
            ' Handle the date selected event for the MonthCalendar, and raise
            ' the ValueChanged event.
            AddHandler Me.myCalendar.DateSelected, AddressOf DateSelectedHandler
    
            '' Handle a click on the parent form to hide the MonthCalendar.
            AddHandler CType(Me.Parent, Form).Click, AddressOf LostFocusHandler
    
            '' Handle losing focus to hide the MonthCalendar.
            AddHandler Me.myCalendar.Leave, AddressOf LostFocusHandler
    
            '' Transfer settings to the internal month calendar control.
            Me.myCalendar.Font = Me.CalendarFont
            Me.myCalendar.ForeColor = Me.CalendarForeColor
            Me.myCalendar.BackColor = Me.CalendarMonthBackground
            Me.myCalendar.TitleBackColor = Me.CalendarTitleBackColor
            Me.myCalendar.TitleForeColor = Me.CalendarTitleForeColor
            Me.myCalendar.TrailingForeColor = Me.CalendarTrailingForeColor
    
    
    
            '' Make sure that the MonthCalendar is added to parent form.
            If Not Me.Parent Is Nothing AndAlso Me.myCalendar.Parent Is Nothing Then
    
                ' Set the location to just below the DateTimePicker.
                Me.myCalendar.Location = New Point(Me.Left, Me.Top + Me.Height)
    
                Me.Parent.Controls.Add(Me.myCalendar)
            End If
    
            ' Show the MonthCalendar.
            Me.myCalendar.Visible = True
            Me.myCalendar.Focus()
    
        End Sub  'DropDownCalendar
    
    
        Private Function LoWord(ByVal dwValue As Int32) As Int16
            Return dwValue And &HFFFF
        End Function
    
        Private Function HiWord(ByVal dwValue As Int32) As Int16
            Return (dwValue >> 16) And &HFFFF
        End Function
    
    End Class

    然后在winform中同MonthCalendar一样赋date数组给BoldedDates属性即可。

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dateFirst As Date = New Date(2014, 3, 11)
            SpecialDatePicker1.BoldDates = New Date() {dateFirst}
        End Sub
    End Class

    效果图:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2014年4月3日 7:43
    版主

全部回复

  • MonthCalendar能够以粗体显示特定日期,DateTimePicker如何实现?


    韩立学

    DateTimePicker 不支持MonthCalendar 设置粗体时所使用BoldedDates属性。所以你得自己重写这个类来拓展来实现这个效果。

    remember make the reply as answer and vote the reply as helpful if it helps.

    2014年4月3日 2:33
  • MonthCalendar能够以粗体显示特定日期,DateTimePicker如何实现?


    韩立学

    DateTimePicker 不支持MonthCalendar 设置粗体时所使用BoldedDates属性。所以你得自己重写这个类来拓展来实现这个效果。

    remember make the reply as answer and vote the reply as helpful if it helps.

    Hi,

    正如Risa所说,DateTimePicker没有BoldedDates属性, 我们可以自己拓展DateTimePicker类来实现这样的功能。

    Imports System.ComponentModel
    
    Public Class SpecialDatePicker : Inherits DateTimePicker
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    
            Const WM_LBUTTONDOWN& = &H201
            ' Detect a left-button click on the control.
            If m.Msg = WM_LBUTTONDOWN Then
                ' Show the internal month calendar.
                Me.DropDownCalendar()
            Else
                MyBase.WndProc(m)
            End If
    
        End Sub
    
        Private my_calendar As New MonthCalendar With {
            .MaxSelectionCount = 1}        'limit it in single seletet mode
    
    
        <Browsable(False)> _
        Public ReadOnly Property MyCalendar() As MonthCalendar
            Get
                Return my_calendar
            End Get
        End Property  'Calendar
    
    
        Public Property BoldDates() As Date()
            Get
                Return Me.MyCalendar.BoldedDates
            End Get
            Set(ByVal Value As Date())
                Me.MyCalendar.BoldedDates = Value
            End Set
        End Property  'BoldDates
    
    
        Private Sub DateSelectedHandler(ByVal sender As Object, ByVal e As DateRangeEventArgs)
            Me.Value = Me.myCalendar.SelectionStart.ToShortDateString
            Me.myCalendar.Visible = False
        End Sub  'DateSelectedHandler
    
    
        Private Sub LostFocusHandler(ByVal sender As Object, ByVal e As EventArgs)
    
            Me.myCalendar.Visible = False
    
            MyBase.OnLeave(e)
    
        End Sub  'LostFocusHandler
    
    
        Private Sub DropDownCalendar()
            'Return
            ' Handle the date selected event for the MonthCalendar, and raise
            ' the ValueChanged event.
            AddHandler Me.myCalendar.DateSelected, AddressOf DateSelectedHandler
    
            '' Handle a click on the parent form to hide the MonthCalendar.
            AddHandler CType(Me.Parent, Form).Click, AddressOf LostFocusHandler
    
            '' Handle losing focus to hide the MonthCalendar.
            AddHandler Me.myCalendar.Leave, AddressOf LostFocusHandler
    
            '' Transfer settings to the internal month calendar control.
            Me.myCalendar.Font = Me.CalendarFont
            Me.myCalendar.ForeColor = Me.CalendarForeColor
            Me.myCalendar.BackColor = Me.CalendarMonthBackground
            Me.myCalendar.TitleBackColor = Me.CalendarTitleBackColor
            Me.myCalendar.TitleForeColor = Me.CalendarTitleForeColor
            Me.myCalendar.TrailingForeColor = Me.CalendarTrailingForeColor
    
    
    
            '' Make sure that the MonthCalendar is added to parent form.
            If Not Me.Parent Is Nothing AndAlso Me.myCalendar.Parent Is Nothing Then
    
                ' Set the location to just below the DateTimePicker.
                Me.myCalendar.Location = New Point(Me.Left, Me.Top + Me.Height)
    
                Me.Parent.Controls.Add(Me.myCalendar)
            End If
    
            ' Show the MonthCalendar.
            Me.myCalendar.Visible = True
            Me.myCalendar.Focus()
    
        End Sub  'DropDownCalendar
    
    
        Private Function LoWord(ByVal dwValue As Int32) As Int16
            Return dwValue And &HFFFF
        End Function
    
        Private Function HiWord(ByVal dwValue As Int32) As Int16
            Return (dwValue >> 16) And &HFFFF
        End Function
    
    End Class

    然后在winform中同MonthCalendar一样赋date数组给BoldedDates属性即可。

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dateFirst As Date = New Date(2014, 3, 11)
            SpecialDatePicker1.BoldDates = New Date() {dateFirst}
        End Sub
    End Class

    效果图:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2014年4月3日 7:43
    版主