可为空的日期控件

.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

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

zwq78

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值