winform C# 实现toast效果(窗体定时自我关闭)

近期新做一个项目(winform C#),保存信息时想实现类似于Android中Toast的效果,也就是弹出提示信息之后信息框自动关闭。

周五网上找了一圈并自己尝试无果。

周六下午用英文搜索一下子就找到了方案,废话不多说,代码如下:

private System.Windows.Forms.Timer timer;

timer = new System.Windows.Forms.Timer();
timer.Tick += delegate {
     this.Close();
};
timer.Interval = (int)TimeSpan.FromSeconds(2).TotalMilliseconds;
timer.Start();

原文地址:https://stackoverflow.com/questions/45146043/c-sharp-automatically-close-a-form-after-x-minutes

总体的思路是:新建一个窗体并Show()出来,当然,它的样式要整理一下,让它看上去不像个Form,然后开启定时关闭的timer。

注意,这里用的是System.Windows.Forms.Timer实现,之前用了 System.Timers.Timer,但Elapsed事件不好处理,总提示跨线程调用或者句柄的问题,所以放弃它。

效果如下:

猜你喜欢

转载自blog.csdn.net/zhouyingge1104/article/details/88368074