0

How can I show a message in the statusBar for 5 seconds?
I do some code, on the end I write out a message in statusBar (statusBar.Text = "Notification Message"; ).
But I want that is shown for only 5 seconds. Then it dissapears (with statusBar.Text = ""; )

How can I do this?

Edited by Mitja Bonca: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by sknake
0

Got to. It works:

public class Form1 : Form {//Do something like following in your form
    System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    StatusBar statusBar1 = new StatusBar();

    // This is the method to run when the timer is raised.
    private void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) {
       myTimer.Stop();
       statusBar1.Text="";
       myTimer.Tick -= TimerEventProcessor;
     }

    public void SetStatusBar() {
       /* Adds the event and the event handler for the method that will 
          process the timer event to the timer. */
       myTimer.Tick += new EventHandler(TimerEventProcessor);

       // Sets the timer interval to 5 seconds.
       myTimer.Interval = 5000;

       statusBar1.Text="Notification Message";
       myTimer.Start();
    }
 }
0

You may find this code a little easier to work with:

private void button1_Click(object sender, EventArgs e)
    {
      new Action<Control, string, int>(SetText).BeginInvoke(label1, "Notification Message", 5, null, null);
    }

    private void SetText(Control ctrl, string txt, int seconds)
    {
      this.Invoke(new MethodInvoker(
        delegate()
        {
          ctrl.Text = txt;
        }));
      System.Threading.Thread.Sleep(seconds * 1000);
      this.Invoke(new MethodInvoker(
        delegate()
        {
          ctrl.Text = string.Empty;
        }));
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.