這應該是相當直接的,但我無法弄清楚,
我在表單上有一個按鈕,默認背景色為銀色,按下按鈕時它會變成紅色,
如果背景色是紅色并且達到一天中的某個時間,我希望背景色變為黑色。我使用的代碼如下
If CAButton.BackColor = Color.Red And DateTime.Now > #11:48:00# Then
CAButton.BackColor = Color.Black
但是現在出于某種原因,現在幾點都沒有關系,只要我按下按鈕,它就會自動變為黑色,顯然代碼中缺少一些東西,但我無法說出是什么,誰能看出哪里出了問題?
uj5u.com熱心網友回復:
Now是帶時間的完整日期。您需要將其與完整的日期時間進行比較。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.BackColor = Color.Red And DateTime.Now > New DateTime(Now.Year, Now.Month, Now.Day, 11, 48, 0) Then
Button1.BackColor = Color.Black
End If
End Sub
uj5u.com熱心網友回復:
除了 Mary 的回答之外,您還可以只檢查TimeOfDayfromDateTime.Now與 a TimeSpan:
Private targetTime As New TimeSpan(11, 48, 0)
Private Sub CAButton_Click(sender As Object, e As EventArgs) Handles CAButton.Click
If CAButton.BackColor = Color.Red And DateTime.Now.TimeOfDay >= targetTime Then
CAButton.BackColor = Color.Black
End If
End Sub
uj5u.com熱心網友回復:
您可以使用設定為在所需時間觸發的計時器來實作這一點,您可以在單擊按鈕時進行設定。
我使用以下代碼在新的 Windows 表單專案中的新表單上放置了一個名為 CAButton 的按鈕:
Public Class Form1
Private caBlackTime As New TimeSpan(19, 0, 0)
Private tim As Timer = Nothing
Private Sub Tim_Tick(sender As Object, e As EventArgs)
tim.Stop()
If CAButton.BackColor = Color.Red Then
CAButton.BackColor = Color.Black
End If
End Sub
Private Sub CAButton_Click(sender As Object, e As EventArgs) Handles CAButton.Click
If tim IsNot Nothing Then tim.Dispose()
Dim timeUntilChangeColour = caBlackTime - DateTime.Now.TimeOfDay
If timeUntilChangeColour > TimeSpan.Zero Then
CAButton.BackColor = Color.Red
tim = New Timer With {.Interval = CInt(timeUntilChangeColour.TotalMilliseconds)}
AddHandler tim.Tick, AddressOf Tim_Tick
tim.Start()
Else
CAButton.BackColor = Color.Black
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Tidy up
If tim IsNot Nothing Then tim.Dispose()
End Sub
End Class
有多種 Timer 可用,但這種是 Windows.Forms.Timer,它最適合這種特殊情況。
uj5u.com熱心網友回復:
啊,@AndrewMorton 打敗了我!好吧,我不會讓所有這些代碼浪費掉......
這是一個設定顏色然后自動設定顏色的解決方案,無需單擊任何按鈕,每天以指定的時間間隔。是的,每天 - 如果應用程式保持打開狀態,那么它將在明天和第二天發生,依此類推。每種顏色的顏色和時間是可配置的。它還處理用戶啟動應用程式時時間已經過去的情況,以及用戶啟動應用程式在紅色和黑色之間的情況。我System.Threading.Timer這里用。注意:在 .NET 中使用任何計時器的準確性可能會隨著時間的推移而漂移。
' declare the timers, will be initialized in Form_Load
Private turnColor1Timer As System.Threading.Timer
Private turnColor2Timer As System.Threading.Timer
' set the two colors at this level for a single point of configuration
Private ReadOnly color1 As Color = Color.Red
Private ReadOnly color2 As Color = Color.Black
' first color time is set here for ease of configuration
Private ReadOnly color1TimeOfDay As New TimeSpan(11, 48, 0)
' second color time is set here based on previous color some timespan, for ease of configuration
Private ReadOnly color2TimeOfDay As New TimeSpan(color1TimeOfDay.Add(New TimeSpan(0, 1, 0)).Ticks)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' not sure if you want initial state to be black?
turnColorTimerTick(color2)
' initialize the times to change colors to today
Dim turnColor1Time = DateTime.Now.Date.Add(color1TimeOfDay)
Dim turnColor2Time = DateTime.Now.Date.Add(color2TimeOfDay)
' check if that time has already passed today, if so set to tomorrow and change color
If turnColor1Time.CompareTo(DateTime.Now) < 0 Then
turnColorTimerTick(color1)
turnColor1Time = turnColor1Time.AddDays(1)
End If
If turnColor2Time.CompareTo(DateTime.Now) < 0 Then
turnColorTimerTick(color2)
turnColor2Time = turnColor2Time.AddDays(1)
End If
' initialize both timers to their appropriate times
turnColor1Timer = New System.Threading.Timer(AddressOf turnColorTimerTick, color1, CInt(turnColor1Time.Subtract(DateTime.Now).TotalMilliseconds), CInt(TimeSpan.FromDays(1).TotalMilliseconds))
turnColor2Timer = New System.Threading.Timer(AddressOf turnColorTimerTick, color2, CInt(turnColor2Time.Subtract(DateTime.Now).TotalMilliseconds), CInt(TimeSpan.FromDays(1).TotalMilliseconds))
End Sub
Private Sub turnColorTimerTick(state As Object)
' since it ticks in a background thread, must check if you need to invoke back to UI and call Invoke as needed
If CAButton.InvokeRequired Then
CAButton.Invoke(New Action(Of Object)(AddressOf turnColorTimerTick), {state})
Else
' get color from argument passed
Dim color = DirectCast(state, Color)
CAButton.BackColor = color
End If
End Sub
' this is put here because the timers should be disposed of
' if you choose to do this, you must remove Dispose from Form.Designer code
' (compiler error will alert you of this if you don't)
' of course, you could just modify the Dispose in Form.Designer,
' but I like to keep any code I have modified in the Form code instead
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing Then
components?.Dispose()
End If
turnColor1Timer?.Dispose()
turnColor2Timer?.Dispose()
Finally
MyBase.Dispose(disposing)
End Try
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/378911.html
標籤:网络
