22

I have buttons which validate if the user is administrator or not. If the user currently login is not an administrator then label will show as warning message and then hide after a few seconds. I tried using lblWarning.Hide(); and lblWarning.Dispose(); after the warning message, but the problem is, it hides the message before even showing the warning message. This is my code.

private void button6_Click(object sender, EventArgs e)
{
    if (txtLog.Text=="administrator")
    {
        Dialog();
    }

    else
    {
       lblWarning.Text = "This action is for administrator only.";
       lblWarning.Hide();
    }

}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
user2262382
  • 335
  • 2
  • 5
  • 13
  • In your code you are hiding just after setting the text. you need to wait for a while like for some seconds. you just want to show the message as notifications anywhere on the page. I would use toastr or similar functionality.https://github.com/CodeSeven/toastr – KKS Apr 11 '13 at 14:45
  • you can use javascript to show the message to the client and than hide it, it should not be done in the server's code. see: http://stackoverflow.com/questions/4634013/javascript-sleep – omer schleifer Apr 11 '13 at 14:46

5 Answers5

38

You're going to want to "hide" it with a Timer. You might implement something like this:

var t = new Timer();
t.Interval = 3000; // it will Tick in 3 seconds
t.Tick += (s, e) =>
{
    lblWarning.Hide();
    t.Stop();
};
t.Start();

instead of this:

lblWarning.Hide();

so if you wanted it visible for more than 3 seconds then just take the time you want and multiply it by 1000 because Interval is in milliseconds.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • what is e in that code is that the variable for the lblWarning or is that represent the time? – user2262382 Apr 11 '13 at 15:05
  • @user2262382, it's an `EventArgs` object. The `Tick` event uses the [`EventHandler`](http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx) delegate and it has two arguments `Object sender, EventArgs e`. So, I'm declaring an inline delegate for those parameters. – Mike Perrenoud Apr 11 '13 at 15:07
  • I got it. I have an exception for the variable e because I already use it from the other codes. – user2262382 Apr 11 '13 at 15:07
  • @user2262382, ok. Just use a different name, it doesn't really matter, as you can see I don't even use it. – Mike Perrenoud Apr 11 '13 at 15:08
  • another problem occur it shows the message but this will not hide even a few seconds. – user2262382 Apr 11 '13 at 15:10
  • @user2262382, ha! I forgot to call `Start`! A common mistake. Have a look at my edit. – Mike Perrenoud Apr 11 '13 at 15:12
  • it is perfectly working thank you very much. Any way you also forgot the semicolon after the closing bracket. – user2262382 Apr 11 '13 at 15:20
  • 1
    It's worth noting that the `Timer` described here is `System.Timers.Timer` since there are timers also in `System.Threading` and `System.Windows.Forms`. – Moshisho Nov 30 '15 at 09:27
  • 1
    @Moshisho Timer.Tick is not in the Systems.Timers.Timer class, i have had to use System.Windows.Forms. I am not sure if this is because of a most recent version of visual studio or C# but tick is in System.Windows.Forms now – Brendon May 11 '16 at 15:59
  • 2
    @Moshisho, this is a `System.Windows.Forms.Timer` because this solution was specifically around a Windows form. – Mike Perrenoud May 17 '16 at 20:43
  • 3
    @Brendon @Mike, For the time being I can't recall why I wrote that the Timer is in `System.Timers.Timer` and not `System.Windows.Forms.Timer`, probably by mistake. – Moshisho May 18 '16 at 06:55
  • I'd recommend the forms timer since it's on the same thread as the GUI (in a winforms app) so you aren't going to have cross threading problems. – Jeff Aug 02 '18 at 23:18
2

If you are using UWP XAML in 2020 and your msgSaved label is a TextBlock, you could use the code below:

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(2);
msgSaved.Visibility = Visibility.Visible;
timer.Tick += (s, en) => {
        msgSaved.Visibility = Visibility.Collapsed;
        timer.Stop(); // Stop the timer
    };
timer.Start(); // Starts the timer. 
ZP007
  • 582
  • 7
  • 12
0

Surely you could just use Thread.Sleep

lblWarning.Text = "This action is for administrator only.";
System.Threading.Thread.Sleep(5000);
lblWarning.Hide();

Where 5000 = the number of miliseconds you want to pause/wait/sleep

Nigel B
  • 3,577
  • 3
  • 34
  • 52
0

The following solution works for wpf applications. When you start timer a separate thread is started. To update UI from that thread you have to use dispatch method. Please the read the comments in code and use code accordingly. Required header

using System.Timers;

private void DisplayWarning(String message, int Interval = 3000)
    {
        Timer timer = new Timer();
        timer.Interval = Interval;
        lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Content = message));
        lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Visibility = Visibility.Visible));

        // above two line sets the visibility and shows the message and interval elapses hide the visibility of the label. Elapsed will we called after Start() method.

        timer.Elapsed += (s, en) => {
            lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Visibility = Visibility.Hidden));
            timer.Stop(); // Stop the timer(otherwise keeps on calling)
        };
        timer.Start(); // Starts the timer. 
    }

Usage :

DisplayWarning("Warning message"); // from your code
SRIDHARAN
  • 1,196
  • 1
  • 15
  • 35
0

this function display specific msg on an label for specific time duration including text style

public void show_MSG(string msg, Color color, int d)
    {
        this.Label.Visible = true;
        this.Label.Text = msg;
        this.Label.ForeColor = color;
        Timer timer = new Timer();
        timer.Interval = d;
        timer.Tick += (object sender, EventArgs e) =>
        {
            this.Label.Visible = false;
        }; timer.Start();
    
    }