(高分悬赏)用vb.net 做个类似DateTimePicker控件的东西,有调整日期的下拉菜单,但是时间要会动的
精彩的答案还加分 展开
继承一下DateTimePicker,内置个timer 不断刷新显示
得到焦点停止timer,失去焦点继续timer
把代码贴到项目就会多出一个控件了
拖出来就能使用
显示格式可以自己设置
---------------------------------------------------------------------------------
''' <summary>
''' 自动更新的 DateTimePacker
''' </summary>
Public Class MyDateTimePacker
Inherits DateTimePicker
Public Sub New()
Me.components = New Container
Me.m_tmrUpdate = New Timer(Me.components)
Me.m_tmrUpdate.Interval = 1000
AddHandler Me.m_tmrUpdate.Tick, New EventHandler(AddressOf Me.m_tmrUpdate_Tick)
MyBase.Format = DateTimePickerFormat.Custom
MyBase.CustomFormat = "yyyy 年 MM 月 dd 日 hh:mm:ss"
End Sub
''' <summary>
''' 释放资源
''' </summary>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If (disposing AndAlso (Not Me.components Is Nothing)) Then
Me.components.Dispose
End If
MyBase.Dispose(disposing)
End Sub
Private Sub m_tmrUpdate_Tick(ByVal sender As Object, ByVal e As EventArgs)
MyBase.Value = DateTime.Now
End Sub
Protected Overrides Sub OnEnter(ByVal e As EventArgs)
If Me.AutoUpdateByFocu Then
Me.m_tmrUpdate.Stop
End If
MyBase.OnEnter(e)
End Sub
Protected Overrides Sub OnLeave(ByVal e As EventArgs)
If Me.AutoUpdateByFocu Then
Me.m_tmrUpdate.Start
End If
MyBase.OnLeave(e)
End Sub
' Properties
''' <summary>
''' 更新定时器状态
''' </summary>
Public Property AutoUpdate As Boolean
Get
Return Me.m_tmrUpdate.Enabled
End Get
Set(ByVal value As Boolean)
Me.m_tmrUpdate.Enabled = value
End Set
End Property
''' <summary>
''' 根据焦点状态开始、停止更新定时器
''' </summary>
<DefaultValue(False)> _
Public Property AutoUpdateByFocu As Boolean
Get
Set(ByVal value As Boolean)
End Property
''' <summary>
''' 组件容器
''' </summary>
Private components As IContainer = Nothing
''' <summary>
''' 更新定时器
''' </summary>
Private m_tmrUpdate As Timer
End Class
---------------------------------------------------------------------------------
(用C#写的,然后反编译成VB.NET,有图有真相-.-真蛋疼,学C#吧...)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "yyyy年MM月dd日 dddd HH时mm分ss秒"
Timer1.Interval = 500
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DateTimePicker1.CustomFormat = "yyyy年MM月dd日 dddd HH时mm分ss秒"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DateTimePicker1.CustomFormat = "HH:mm:ss"
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
DateTimePicker1.Value = Now
End Sub
End Class
还是喜欢原装进口的