简体   繁体   English

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

[英]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();我尝试使用lblWarning.Hide(); and lblWarning.Dispose();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();
    }

}

You're going to want to "hide" it with a Timer .你会想用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.因此,如果您希望它的可见时间超过 3 秒,那么只需花费您想要的时间并将其乘以 1000,因为Interval以毫秒为单位。

If you are using UWP XAML in 2020 and your msgSaved label is a TextBlock, you could use the code below:如果你在 2020 年使用 UWP XAML 并且你的 msgSaved 标签是一个 TextBlock,你可以使用下面的代码:

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. 

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 = 您要暂停/等待/睡眠的毫秒数

The following solution works for wpf applications.以下解决方案适用于 wpf 应用程序。 When you start timer a separate thread is started.当您启动计时器时,将启动一个单独的线程。 To update UI from that thread you have to use dispatch method.要从该线程更新 UI,您必须使用 dispatch 方法。 Please the read the comments in code and use code accordingly.请阅读代码中的注释并相应地使用代码。 Required header必需的标题

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM