使控件仅在c#中的特定时间内可见

Cde*_*eez 0 c# controls

我正在开发一个应用程序,我需要以下要求:

假设我的表单中有一个按钮和一个标签(最初的可见性设置为false),并且用户点击了该按钮,那么标签应该显示一些文本,我在按钮点击中分配给标签.但是这个标签应该只显示一段时间,比如大约3秒,然后它应该自动隐藏.为此,如果我给:

private void button1_Click(object sender, EventArgs e)
    {
     label1.Visible=true;
     label1.Text= "Magic";
     Thread.Sleep(3000);
     label1.Visible=false;
    }
Run Code Online (Sandbox Code Playgroud)

此代码无助于此目的.这样做的方法是什么?

Dan*_*iel 5

尝试使用以下方法替换方法的最后两行:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 3000;
timer.Tick += (source, e) => {label1.Visible = false; timer.Stop();};
timer.Start();
Run Code Online (Sandbox Code Playgroud)

在WinForms中使用Thread.Sleep()绝不是一个好主意; 改为使用计时器.