我有这个问题,当我点击程序中的按钮时,我想设置一个这样的计时器:
第一个文本标签"Not Connected"
(红色)变为"Verifying"
(绿色),一段时间后它永久变为"Connected"
(绿色)
我怎么做 ???
我有这个问题,当我点击程序中的按钮时,我想设置一个这样的计时器:
第一个文本标签"Not Connected"
(红色)变为"Verifying"
(绿色),一段时间后它永久变为"Connected"
(绿色)
我怎么做 ???
由于您没有提供带有计时器的代码来了解您要做什么,因此我无法给出更好的答案,您可以调整我所做的这段代码:
Public Class Form1
Private Enum State
NotConnected = 141 ' Red
Verifying = 53 ' DarkGreen
Connected = 79 ' Green
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
Select Case Label1.Tag
Case Nothing
SetLabelState(Label1, "Not Connected ", State.NotConnected)
Case State.NotConnected
SetLabelState(Label1, "Verifying", State.Verifying)
Case State.Verifying
SetLabelState(Label1, "Connected", State.Connected)
Case State.Connected
' Do nothing here
Case Else
Throw New Exception("Select case is out of index")
End Select
End Sub
Private Sub SetLabelState(ByVal lbl As Label, _
ByVal txt As String, _
ByVal col As State)
lbl.BackColor = Color.FromKnownColor(CType(col, KnownColor))
lbl.Tag = col
lbl.Text = txt
End Sub
End Class
你可以Timer
在这里使用类。这是我在代码中实现的->
//click event on the button to change the color of the label
public void buttonColor_Click(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 500;// Timer with 500 milliseconds
timer.Enabled = false;
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
//label text changes from 'Not Connected' to 'Verifying'
if (labelFirst.BackColor == Color.Red)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Verifying";
}
//label text changes from 'Verifying' to 'Connected'
else if (labelFirst.BackColor == Color.Green)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Connected";
}
//initial Condition (will execute)
else
{
labelFirst.BackColor = Color.Red;
labelFirst.Text = "Not Connected";
}
}