在一段时间内更改按钮的颜色

时间:2018-06-13 19:28:14

标签: vb.net button timer

我有一个小的VB.net应用程序,有很多按钮。我需要在点击按钮时更改按钮的背面颜色,然后在10秒后将其设置回原始颜色。我正在努力使用计时器或时间过程两者都有自己的问题。

任何使这项工作和工作有效的想法?

代码:

Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click,
Button2.Click

    Dim myButton = DirectCast(sender, Button)
    MakeCall()
    myButton.BackColor = Color.Green
    'TurnOnActiveCallCOLOR.Enabled = True
    For i As Integer = 0 To 10000 - 1
        Threading.Thread.Sleep(10000)
    Next
    myButton.BackColor = Color.FromArgb(64, 64, 64)
End Sub

5 个答案:

答案 0 :(得分:2)

以下是使用Windows窗体计时器完成所需操作的示例:

Private MyButton As Button

Private Sub MyButtons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
   MyButton = DirectCast(sender, Button)
   MyButton.BackColor = Color.Green

   Timer1.Enabled = True

   MakeCall()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
   MyButton.BackColor = Color.FromArgb(64, 64, 64)
   Timer1.Enabled = False
End Sub

答案 1 :(得分:2)

要处理任意数量的按钮,您可以使用以下内容:

>>> dd = DummyClass()

>>> dd["one"] = {"two": 2}
Uses my __setitem__ one {'two': 2}
Uses my __setitem__ two 2

>>> dd["one"].update({"two": 4})
Doing some stuff here
Uses my __setitem__ two 4

>>> print(dd)
{'one': {'two': 4}}

如果在超时之前再次单击按钮,则重新开始时间。

如果您想为不同的按钮设置不同的超时/颜色,可以添加其他构造函数。

Public Class Form1 Dim buttonTimers As New Dictionary(Of Control, ButtonTimer) Class ButtonTimer Property Timeout As Integer = 2000 Property Target As Control Property ActiveColor As Color = Color.Green Property DefaultColor As Color = Color.FromArgb(64, 64, 64) Private tim As Timer Sub TimTick(sender As Object, e As EventArgs) tim.Stop() Target.BackColor = DefaultColor End Sub Sub New() ' empty constructor End Sub Sub New(target As Control) Me.Target = target Me.Target.BackColor = Me.ActiveColor tim = New Timer With {.Interval = Timeout} AddHandler tim.Tick, AddressOf TimTick tim.Start() End Sub Sub Restart() Target.BackColor = Me.ActiveColor If tim IsNot Nothing Then tim.Stop() tim.Start() End If End Sub Public Sub DisposeOfTimer() If tim IsNot Nothing Then tim.Stop() RemoveHandler tim.Tick, AddressOf TimTick tim.Dispose() End If End Sub End Class Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click Dim myButton = DirectCast(sender, Button) 'MakeCall() If buttonTimers.ContainsKey(myButton) Then buttonTimers(myButton).Restart() Else buttonTimers.Add(myButton, New ButtonTimer(myButton)) End If End Sub Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing For Each x In buttonTimers x.Value.DisposeOfTimer() Next End Sub End Class 代码应该包含在表单关闭处理程序中(如果有的话),以便正确清理计时器。

我希望自己制作一个继承自Button的自定义按钮类更整洁,所以你可能想要调查那样做。 (How to: Inherit from Existing Windows Forms Controls。)

答案 2 :(得分:1)

使用lambda expressions(以及查询表,如果你想进一步与它进行交互),你可以很容易地做到这一点:

'Lookup table for if you want to be able to interact with the timers even more.
Dim ButtonTimers As New Dictionary(Of Button, Timer)

Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click, Button2.Click
    MakeCall()

    Dim myButton = DirectCast(sender, Button)
    myButton.BackColor = Color.Green

    'If a timer already exists for the button, restart it.
    Dim existingTimer As Timer = Nothing
    If ButtonTimers.TryGetValue(myButton, existingTimer) Then
        existingTimer.Stop()
        existingTimer.Start()
        Return 'Do not execute the rest of the code.
    End If

    'Create the timer and set its Interval to 10000 ms (10 seconds).
    Dim buttonTimer As New Timer() With {.Interval = 10000}

    'Add a handler to its Tick event.
    AddHandler buttonTimer.Tick, _
        Sub(tsender As Object, te As EventArgs)
            myButton.BackColor = Color.FromArgb(64, 64, 64)

            'Dispose timer and remove from lookup table.
            ButtonTimers.Remove(myButton)
            buttonTimer.Stop()
            buttonTimer.Dispose()
        End Sub

    ButtonTimers.Add(myButton, buttonTimer)
    buttonTimer.Start()
End Sub

如果您想与按钮的计时器(如果存在)进行交互,您可以这样做:

Dim buttonTimer As Timer = Nothing
If ButtonTimers.TryGetValue(yourButtonHere) Then
    'Do something with buttonTimer...
End If

答案 3 :(得分:1)

如果您不想使用计时器,请等待asynh等待。使用task.delay的简单方法

Private Async Sub ButtonClick(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
    DirectCast(sender, Button).BackColor = Color.Red 'after click setcolor to red
    Await setColorAfterDelay(sender) 'Add this comand on button click and don't forget add asynh before sub in this method
End Sub

Public Async Function setColorAfterDelay(sender As Object) As Task
    Await Task.Delay(1000) ''Milisecound how long you wana dealey
    DirectCast(sender, Button).BackColor = Color.White 'and set colorto white
End Function

答案 4 :(得分:0)

这可以通过动态创建计时器控件来更好地完成,因此每个按钮都有自己的计时器,但这就是我想出的。

  

哦,将你的计时器设置为1000区间并启用'False'。

Public Class Form1
    Dim T1 As Integer = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        T1 = T1 + 1
        If T1 = 10 Then
            For Each button In Controls
                button.backcolor = Color.FromArgb(225, 225, 225)
            Next
            Timer1.Stop()
            T1 = 0
        End If
        Me.Text = T1
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.BackColor = Color.Red
        Timer1.Start()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Button2.BackColor = Color.Orange
        Timer1.Start()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Button3.BackColor = Color.Yellow
        Timer1.Start()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Button4.BackColor = Color.Green
        Timer1.Start()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Button5.BackColor = Color.Blue
        Timer1.Start()
    End Sub
End Class