還要設置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控件用在這種地方也不適合。(假如你所統計的時間很短,在幾分中內話,可以使用,假如你統計的時間很長:幾小時、幾天幾夜,建議改用以下方式):
在你需要開始計時的地方加入一個記錄當前時間,在你想結束的地方也得到一個當前時間。然后將兩個時間相減。
希望以上思路可以幫到你。
新聞標題:包含vb.net選擇時間控件的詞條
網站路徑:http://vcdvsql.cn/article46/ddccohg.html
成都網站建設公司_創新互聯,為您提供虛擬主機、App設計、網站建設、動態網站、移動網站建設、品牌網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯