十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
还要设置Format 为 : Custom
成都创新互联主要从事成都做网站、网站建设、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务忠县,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
用的时候用DateTimePicker1.Text 不要用value
下面是我随便用了两个DateTimePicker和一个button一个textbox演示了一下的代码,你根据自己的需要调试DateTimePicker风格
------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form
#Region
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents DateTimePicker2 As System.Windows.Forms.DateTimePicker
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.DateTimePicker2 = New System.Windows.Forms.DateTimePicker
Me.SuspendLayout()
'
'DateTimePicker1
'
Me.DateTimePicker1.AllowDrop = True
Me.DateTimePicker1.CustomFormat = "yyyy-MM-dd"
Me.DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom
Me.DateTimePicker1.Location = New System.Drawing.Point(56, 24)
Me.DateTimePicker1.Name = "DateTimePicker1"
Me.DateTimePicker1.ShowUpDown = True
Me.DateTimePicker1.Size = New System.Drawing.Size(160, 19)
Me.DateTimePicker1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(144, 128)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(136, 72)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'DateTimePicker2
'
Me.DateTimePicker2.CustomFormat = "yyyy-MM-dd"
Me.DateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom
Me.DateTimePicker2.Location = New System.Drawing.Point(40, 160)
Me.DateTimePicker2.Name = "DateTimePicker2"
Me.DateTimePicker2.Size = New System.Drawing.Size(120, 19)
Me.DateTimePicker2.TabIndex = 3
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.DateTimePicker2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DateTimePicker1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = DateTimePicker1.Text
End Sub
End Class
----------------------------------------------------------
我的操作系统和.net都是日文的,注释的东西我删除了,主要看代码就好了
这段代码你可以建一个空的解决方案,完全复制到里面去
继承一下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#吧...)
这种功能用不到TIMER,TIMER控件用在这种地方也不适合。(假如你所统计的时间很短,在几分中内话,可以使用,假如你统计的时间很长:几小时、几天几夜,建议改用以下方式):
在你需要开始计时的地方加入一个记录当前时间,在你想结束的地方也得到一个当前时间。然后将两个时间相减。
希望以上思路可以帮到你。