可为空的日期控件

.NET中自带的日期控件不能为空,不太方便,根据这个控件,自定义了可为空的日期控件,代码如下:
Public Class myDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker
    Private txt As TextBox
    Public Enum DefautValue
        Empty = 0
        Today = 1
    End Enum

    Private mDefValue As DefautValue = DefautValue.Empty

    Property DefValue() As DefautValue
        Get
            Return Me.mDefValue
        End Get
        Set(ByVal Value As DefautValue)
            mDefValue = Value
        End Set
    End Property

    Public Sub New(ByVal defval As DefautValue)
        txt = New TextBox
        Me.Controls.Add(txt)
        txt.BorderStyle = BorderStyle.None
        txt.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
        txt.Bounds = New Rectangle(0, 0, Me.Width - 20, Me.Height)
        txt.Show()
        Me.DefValue = defval
        If defval = DefautValue.Empty Then
            txt.Text = ""
        Else
            txt.Text = Date2String(Date.Today)
        End If
    End Sub

    Public Sub New()
        txt = New TextBox
        Me.Controls.Add(txt)
        txt.BorderStyle = BorderStyle.None
        txt.AutoSize = False
        txt.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
        txt.Bounds = New Rectangle(0, 0, Me.Width - 20, Me.Height)
        txt.Visible = True
        txt.BringToFront()
        txt.Text = ""
    End Sub

    Public Overrides Property Text() As String
        Get
            Return txt.Text
        End Get
        Set(ByVal val As String)
            If val <> "" AndAlso IsDate(val) AndAlso CDate(val) <= Me.MaxDate AndAlso CDate(val) >= Me.MinDate Then
                Me.Value = CDate(val)
                txt.Text = val
            Else
                If Me.DefValue = DefautValue.Empty Then
                    txt.Text = ""
                ElseIf Me.DefValue = DefautValue.Today Then
                    Me.Value = Date.Today
                    txt.Text = Date2String(Date.Today)
                End If
            End If
        End Set
    End Property

    Private Sub myDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ValueChanged
        txt.Text = Date2String(Me.Value)
    End Sub

    Private Sub myDateTimePicker_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        If txt.Text <> "" AndAlso Not IsDate(txt.Text) Then
            Me.Text = Date2String(txt.Text)
        End If
    End Sub

    Private Sub myDateTimePicker_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.CloseUp
        If clickinBox = False Then

            Dim temp As String = Me.Value
            Me.Value = Me.Value.AddDays(1)
            Me.Value = temp
            'txt.Text = Date2String(Me.Value)

        End If
        clickinBox = False
    End Sub

    Private clickinBox As Boolean = True

    Private Sub myDateTimePicker_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        clickinBox = True
    End Sub
End Class

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页