在C#中自动关闭消息框

我目前正在C#中开发一个应用程序,我在其中显示一个MessageBox。 如何在几秒钟后自动关闭消息框?

您将需要创建自己的Window,代码隐藏包含加载的处理程序和计时器处理程序,如下所示:

private void Window_Loaded(object sender, RoutedEventArgs e) { Timer t = new Timer(); t.Interval = 3000; t.Elapsed += new ElapsedEventHandler(t_Elapsed); t.Start(); } void t_Elapsed(object sender, ElapsedEventArgs e) { this.Dispatcher.Invoke(new Action(()=> { this.Close(); }),null); } 

然后,您可以通过调用ShowDialog()来显示自定义消息框:

 MyWindow w = new MyWindow(); w.ShowDialog(); 

System.Windows.MessageBox.Show()方法有一个重载,它将所有者Window作为第一个参数。 如果我们创建一个不可见的所有者窗口,然后我们在指定的时间后关闭它,它的子消息框也会关闭。

以下是完整的答案: https : //stackoverflow.com/a/20098381/2190520