Show label text as warning message and hide it after a few seconds?

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.


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. 

Tags:

C#

.Net

Winforms