使标签显示3秒钟,然后再次消失

时间:2009-10-03 14:36:10

标签: c# winforms

我怎么能有一个标签说:“注册表更新正确”然后让它在大约2秒钟后消失?

我猜测通过修改.Visible属性,但我无法弄明白。

5 个答案:

答案 0 :(得分:4)

使用Timer类,但将它jazz up以便它可以在触发Tick事件时调用方法。这是通过创建一个继承自Timer类的新类来完成的。下面是具有单个按钮控件的表单代码(btnCallMetLater)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DemoWindowApp
{
    public partial class frmDemo : Form
    {
        public frmDemo()
        {
            InitializeComponent();
        }

        private void btnCallMeLater_Click(object sender, EventArgs e)
        {
            MethodTimer hide = new MethodTimer(hideButton);
            MethodTimer show = new MethodTimer(showButton);

            hide.Interval = 1000;
            show.Interval = 5000;

            hide.Tick += new EventHandler(t_Tick);
            show.Tick += new EventHandler(t_Tick);

            hide.Start(); show.Start();
        }

        private void hideButton()
        {
            this.btnCallMeLater.Visible = false;
        }

        private void showButton()
        {
            this.btnCallMeLater.Visible = true;
        }

        private void t_Tick(object sender, EventArgs e)
        {
            MethodTimer t = (MethodTimer)sender;

            t.Stop();
            t.Method.Invoke();
        }
    }

    internal class MethodTimer:Timer
    {
        public readonly MethodInvoker Method;
        public MethodTimer(MethodInvoker method)
        {
            Method = method;
        }
    }
}

答案 1 :(得分:3)

当你设置标签时,你可以制作一个在2或3秒后超时的定时器,它会调用一个功能来隐藏你的标签。

答案 2 :(得分:1)

创建System.Forms.Timer,持续时间为2秒。将事件处理程序加到Tick事件和处理程序中,将标签的visible属性设置为false(并禁用Timer)

答案 3 :(得分:1)

您需要设置Timer对象并在on timer事件中通过将Visible设置为false来隐藏您的标签。

计时器类: http://msdn.microsoft.com/en-us/library/system.timers.timer(VS.71).aspx

答案 4 :(得分:0)

如果您不介意用户2秒钟内无法执行任何操作,您可以调用Thread.Sleep(2000)。如果他们只是在等待更新,那就没什么区别了。少了很多代码。

相关问题