views:

277

answers:

5

How can I have a label saying: "Registry updates correctly" and then have it dissapear after about 2 seconds?

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

+3  A: 

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.

Scott M.
+1  A: 

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)

Mitch Wheat
+1  A: 

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%28VS.71%29.aspx

Rafal Ziolkowski
+1  A: 

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;
        }
    }
}
J.Hendrix
A: 

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.