C# 将标签文本显示为警告消息并在几秒钟后隐藏它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15951689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:35:28  来源:igfitidea点击:

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

c#.netwinforms

提问by user2262382

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.

我有按钮可以验证用户是否是管理员。如果当前登录的用户不是管理员,则标签将显示为警告消息,然后在几秒钟后隐藏。我尝试在警告消息之后使用lblWarning.Hide();and lblWarning.Dispose();,但问题是,它甚至在显示警告消息之前隐藏了消息。这是我的代码。

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

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

}

采纳答案by Mike Perrenoud

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

你会想用Timer. 你可能会实现这样的事情:

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 Intervalis in milliseconds.

因此,如果您希望它的可见时间超过 3 秒,那么只需花费您想要的时间并将其乘以 1000,因为Interval它以毫秒为单位。

回答by RedEyedMonster

Surely you could just use Thread.Sleep

当然你可以只使用 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

其中 5000 = 您要暂停/等待/睡眠的毫秒数

回答by SRIDHARAN

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

以下解决方案适用于 wpf 应用程序。当您启动计时器时,将启动一个单独的线程。要从该线程更新 UI,您必须使用 dispatch 方法。请阅读代码中的注释并相应地使用代码。必需的标题

using System.Timers;

使用 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