开发者

Make a label appear for 3 seconds, and then disappear again

How can I have a label say开发者_运维技巧ing: "Registry updates correctly" and then have it disappear after about 2 seconds?

I'm guessing by modifying the .Visible property, but I can't figure it out.


Use the Timer class, but jazz it up so that it can call a method when the Tick event is fired. This is done by creating a new class that inherits from the Timer class. Below is the form code which has a single button control (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;
        }
    }
}


when you set your label, you can make a timer that times out after 2 or 3 seconds that calls a function to hide your label.


Create a System.Forms.Timer, with a duration of 2 seconds. Wireup up an event handler to the Tick event and in the handler, set the label's visible property to false (and disable the Timer)


You need to setup Timer object and in on timer event hide Your label by setting Visible to false.

Timer class: http://msdn.microsoft.com/en-us/library/system.timers.timer(VS.71).aspx


Well if you don't mind the user not being able to do anything for 2 seconds, you could just call Thread.Sleep(2000). If they're just waiting for the update anyhow, it's not much of a difference. Lot less code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜