Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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

}
like image 360
user2262382 Avatar asked Apr 11 '13 14:04

user2262382


1 Answers

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.

like image 171
Mike Perrenoud Avatar answered Sep 21 '22 16:09

Mike Perrenoud