Vb.net 通过单击按钮更改文本标签和颜色
我有这个问题,当我点击程序中的按钮时,我想设置一个这样的计时器: 第一个未连接的文本标签颜色红色变为验证颜色绿色,一段时间后,它永久性地变为连接颜色绿色Vb.net 通过单击按钮更改文本标签和颜色,vb.net,visual-studio-2010,visual-studio,colors,Vb.net,Visual Studio 2010,Visual Studio,Colors,我有这个问题,当我点击程序中的按钮时,我想设置一个这样的计时器: 第一个未连接的文本标签颜色红色变为验证颜色绿色,一段时间后,它永久性地变为连接颜色绿色 我该怎么做呢?既然你没有提供带有计时器的代码来理解你想做什么,我无法给出更好的答案,你可以修改我所做的代码: Public Class Form1 Private Enum State NotConnected = 141 ' Red Verifying = 53 ' DarkGreen Connected = 79
我该怎么做呢?既然你没有提供带有计时器的代码来理解你想做什么,我无法给出更好的答案,你可以修改我所做的代码:
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";
}
}