user2262382
user2262382

Reputation: 335

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

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();
    }

}

Upvotes: 22

Views: 43086

Answers (5)

Ahmed Alshubaky
Ahmed Alshubaky

Reputation: 23

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();
    
    }

Upvotes: 0

ZP007
ZP007

Reputation: 652

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. 

Upvotes: 2

SRIDHARAN
SRIDHARAN

Reputation: 1222

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

Upvotes: 0

Nigel B
Nigel B

Reputation: 3597

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

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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.

Upvotes: 38

Related Questions