如何在 c# winforms 中设置标签的内容并在 5 秒后将其重置为 string.empty?

编程入门 行业动态 更新时间:2024-10-27 19:28:13
本文介绍了如何在 c# winforms 中设置标签的内容并在 5 秒后将其重置为 string.empty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我不知道如何做到这一点,我曾尝试弄乱计时器,但到目前为止无济于事.

I've no real idea how to do this and I have tried messing with a timer but to no avail so far.

那我想做什么?

我有一个空白的标签.当某个事件被触发时,我希望标签在 5 秒内显示竞争成功设置",然后我希望它恢复为空白.

I have a label that is blank. When a certain event is triggered I want the label to say "Competition successfully setup" for a period of 5 seconds after which I want it to return to being blank.

这真的可以做到吗??它可以?我玩过计时器,但似乎不太理想.

Surely this can be done?? Can it? I have played around with a timer but I seem to be well off the mark.

非常欢迎任何帮助.我的微弱尝试如下.

Any help would be most welcome. My feeble attempt is below.

private void UpdateLabel(object sender, EventArgs e)
        {
            var timer = new Timer()
                {
                    Interval = 5000,
                };
            timer.Tick += (s, evt) =>
                  lblCompetitionSetupSuccess.Text = "Competition successfully setup";

            timer.Start();

            lblCompetitionSetupSuccess.Text = string.Empty;
        }

推荐答案

换个方式试试:

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "I will vanish in 5 sec";

        var timer = new Timer();
        timer.Interval = 5000;
        timer.Tick += (o, args) => label1.Text = "";
        timer.Start();
    }

首先将标签设置为您希望它显示 5 秒的任何文本

First set the label to whatever text you want it to display for 5 sec

        label1.Text = "I will vanish in 5 sec";

然后设置您的计时器,以便在计时器过去时它将删除文本

Then setup your timer so that on timer elapsed it will remove the text

        var timer = new Timer();
        timer.Interval = 5000;
        timer.Tick += (o, args) => label1.Text = "";
        timer.Start();

如果您希望计时器在第一个计时器结束后停止:

If you want the timer to stop after the first timer elapse:

        timer.Tick += (o, args) =>
            {
                label1.Text = "";
                timer.Enabled = false;
            };

这篇关于如何在 c# winforms 中设置标签的内容并在 5 秒后将其重置为 string.empty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 11:05:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   将其   标签   内容   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!