这是我到目前为止所拥有的
Private Sub BtnAnswerA_Clicked(sender As Object, e As EventArgs) Handles btnAnswerA.Click
Me.btnAnswerA.BackColor = Color.Green
System.Threading.Thread.Sleep(10000)
Me.Hide()
Q2.Show()
End Sub
我想,当点击按钮时,它会改变颜色。然后,10秒后它将打开另一个表格。但它在打开下一个表格之前等待10秒而不改变颜色。
答案 0 :(得分:1)
猜测一下,你可能想要展示其他形式,也许还有不同的延迟。你可以使用一个以表格和延迟为参数的Sub来做到这一点。
假设您使用的是VS2010或更高版本,您的代码可能如下所示:
Private Sub ShowNextForm(formToShow As Form, secondsDelay As Double)
Dim tim As New System.Windows.Forms.Timer
tim.Interval = CInt(secondsDelay * 1000)
tim.Start()
AddHandler tim.Tick, Sub(sender As Object, e As EventArgs)
tim.Stop()
tim.Dispose()
Me.Hide()
formToShow.Show()
End Sub
End Sub
Private Sub btnAnswerA_Click(sender As Object, e As EventArgs) Handles btnAnswerA.Click
Dim bn = DirectCast(sender, Button)
bn.Enabled = False
bn.BackColor = Color.Green
Dim nextForm As New Q2
ShowNextForm(nextForm, 10)
End Sub
然后,如果你有另一个按钮,你想做同样的事情,说“btnAnswer99”,形式为z99,延迟为2.5秒,你只需要编写代码
Private Sub btnAnswer99_Click(sender As Object, e As EventArgs) Handles btnAnswer99.Click
Dim bn = DirectCast(sender, Button)
bn.Enabled = False
bn.BackColor = Color.Blue
Dim nextForm As New z99
ShowNextForm(nextForm, 2.5)
End Sub
获得更高级......可能稍后会看到......
您可能会注意到每个按钮都在重复大量代码。此时,我们应该考虑重构您的代码,以便您只编写一次基本代码,并使用参数来确定它的作用。
所以你看看所有按钮点击处理程序之间的共同点,例如它们都有一个表单,一个要显示的表单,一些要更改的BackColor,以及一个延迟,直到显示下一个表单,以及当然,他们引用 a 按钮。
这是创建 Class 以封装所有部分的理想机会,可能有点像这样:
Friend Class SetButtonToShowForm
Property thisForm As Form
Property nextForm As Form
Property buttonRef As Button
Property buttonBackColor As Color
Property secondsDelay As Double
Friend Sub ShowNextForm(currentForm As Form, formToShow As Form, secondsDelay As Double)
Dim tim As New System.Windows.Forms.Timer
tim.Interval = CInt(secondsDelay * 1000)
tim.Start()
AddHandler tim.Tick, Sub(sender As Object, e As EventArgs)
tim.Stop()
tim.Dispose()
currentForm.Hide()
formToShow.Show()
End Sub
End Sub
Friend Sub bnClick(sender As Object, e As EventArgs)
Dim bn = DirectCast(sender, Button)
bn.Enabled = False
bn.BackColor = Me.buttonBackColor
ShowNextForm(thisForm, nextForm, secondsDelay)
End Sub
Public Sub New()
' empty constructor
End Sub
Public Sub New(sourceForm As Form, targetForm As Form, buttonRef As Button, backColor As Color, secondsDelay As Double)
Me.thisForm = sourceForm
Me.nextForm = targetForm
Me.buttonRef = buttonRef
Me.buttonBackColor = backColor
Me.secondsDelay = secondsDelay
AddHandler Me.buttonRef.Click, AddressOf bnClick
End Sub
End Class
然后你可以按照
的顺序在一个程序中设置所有按钮Private Sub SetButtonHandlers()
Dim b1 As New SetButtonToShowForm(Me, Q2, btnAnswerA, Color.Green, 5)
Dim b2 As New SetButtonToShowForm(Me, z99, btnAnswer99, Color.Blue, 2.3)
End Sub
所以你可以看到,通过小一点点努力,你可以让更多更容易设置更多按钮。
答案 1 :(得分:-3)
参见代码
Private Sub BtnAnswerA_Clicked(sender As Object, e As EventArgs) Handles btnAnswerA.Click
Me.btnAnswerA.BackColor = Color.Green
Application.DoEvents() ' <--- THERE
System.Threading.Thread.Sleep(10000)
Me.Hide()
Q2.Show()
End Sub
无论发生什么,发生得如此之快,您的表单/按钮没有时间重新绘制。在这些情况下,您使用DoEvents
告诉应用程序在处理下一个消息之前完成队列中的所有消息。它完成绘画然后开始睡觉,因此显示一个绿色按钮。
DoEvents带来了意见。我喜欢这个https://stackoverflow.com/a/5183623/1704458
人们对DoEvents
感到非常愤怒,但它从未让我失望过。好。这是我为@Plutonix写的
private _timer As new System.Timers.Timer(10000)
Private Sub Form_Load(sender As Object, e As EventArgs)
AddHandler _timer.Elapsed, AddressOf OnElapsed
End Sub
Private Sub BtnAnswerA_Clicked(sender As Object, e As EventArgs) Handles btnAnswerA.Click
Me.btnAnswerA.BackColor = Color.Green
Me.btnAnswerA.Enabled = False
_timer.Enabled = True
End Sub
Private Sub OnElapsed (sender As Object, e As ElapsedEventArgs)
_timer.Enabled = False
Me.btnAnswerA.Enabled = True
Me.Hide()
Q2.Show()
End Sub
计时器Elapsed
在不同的线程上运行,因此您的表单将响应而不是冻结