C# StatusLabel-如何重置或允许状态文本超时或消失

C# StatusLabel-如何重置或允许状态文本超时或消失,c#,winforms,C#,Winforms,我有一个选项卡式表单,底部有一个StatusStrip,其中包括一个StatusLabel。我想将此状态标签用于各种操作(“1条记录已更新”等)。创建特定事件以设置标签的文本属性非常简单 但如何最好地将状态重置为空白?用户可以在状态不再有意义的情况下执行任意数量的其他操作(转到其他选项卡,单击其他按钮等) 不可能创建所有可能的事件来重置状态消息。有没有办法加入某种类型的计时器,使消息在几秒钟后消失?还有其他人找到了解决这个问题的好办法吗?但是清除状态真的很重要吗?有很多产品将保持其状态标签不变,

我有一个选项卡式表单,底部有一个StatusStrip,其中包括一个StatusLabel。我想将此状态标签用于各种操作(“1条记录已更新”等)。创建特定事件以设置标签的文本属性非常简单

但如何最好地将状态重置为空白?用户可以在状态不再有意义的情况下执行任意数量的其他操作(转到其他选项卡,单击其他按钮等)


不可能创建所有可能的事件来重置状态消息。有没有办法加入某种类型的计时器,使消息在几秒钟后消失?还有其他人找到了解决这个问题的好办法吗?

但是清除状态真的很重要吗?有很多产品将保持其状态标签不变,直到下一个状态事件发生。VisualStudio就是一个很好的例子。简化您的场景并采用这种方法可能是值得的

如果您确实想在事件发生后清除状态,我认为最可维护的方法是使用
计时器。设置状态几秒钟后基本清除

Timer m_timer;

void SetStatus(string text) {
  m_statusLabel.Text = text; 
  m_timer.Reset();
}

void OnTimerTick(object sender, EventArgs e) {
  m_statusLabel.Text = "";
  m_timer.Stop();
}

但是,清除状态真的很重要吗?有很多产品将保持其状态标签不变,直到下一个状态事件发生。VisualStudio就是一个很好的例子。简化您的场景并采用这种方法可能是值得的

如果您确实想在事件发生后清除状态,我认为最可维护的方法是使用
计时器。设置状态几秒钟后基本清除

Timer m_timer;

void SetStatus(string text) {
  m_statusLabel.Text = text; 
  m_timer.Reset();
}

void OnTimerTick(object sender, EventArgs e) {
  m_statusLabel.Text = "";
  m_timer.Stop();
}

是的,一个定时器可以用来清除它。这是一个例子,我已经敲了一起

public partial class Form1 : Form
{
    private System.Timers.Timer _systemTimer = null;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _systemTimer = new System.Timers.Timer(500); 
        _systemTimer.Elapsed += _systemTimer_Elapsed;
    }

    void _systemTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        toolStripStatusLabel1.Text = string.Empty;
        _systemTimer.Stop(); // stop it if you don't want it repeating 
    }

    private void button1_Click(object sender, EventArgs e)
    {
        toolStripStatusLabel1.Text = "random text just as an example";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _systemTimer.Start();

    }
}

假设button1是更新状态的操作,button2只是启动计时器的随机方式(这可以是您想要启动它的方式,我只使用了另一个按钮单击作为示例)。设定的时间过后,状态标签将被清除。

是的,计时器将对此进行清除。这是一个例子,我已经敲了一起

public partial class Form1 : Form
{
    private System.Timers.Timer _systemTimer = null;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _systemTimer = new System.Timers.Timer(500); 
        _systemTimer.Elapsed += _systemTimer_Elapsed;
    }

    void _systemTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        toolStripStatusLabel1.Text = string.Empty;
        _systemTimer.Stop(); // stop it if you don't want it repeating 
    }

    private void button1_Click(object sender, EventArgs e)
    {
        toolStripStatusLabel1.Text = "random text just as an example";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _systemTimer.Start();

    }
}

假设button1是更新状态的操作,button2只是启动计时器的随机方式(这可以是您想要启动它的方式,我只使用了另一个按钮单击作为示例)。设定的时间过后,状态标签将被清除。

感谢您的快速响应!我确实实现了一个计时器;然而,我认为MattR的答案是可以接受的,因为他给出了一个更完整的例子。这个答案阻止了我实现一些东西来清除显示的标签。我在犹豫。干杯感谢您的快速回复!我确实实现了一个计时器;然而,我认为MattR的答案是可以接受的,因为他给出了一个更完整的例子。这个答案阻止了我实现一些东西来清除显示的标签。我在犹豫。干杯