在C#中自动关闭消息框 [英] Automatically close messagebox in C#

查看:468
本文介绍了在C#中自动关闭消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发在C#中,我显示一个MessageBox的应用程序。我怎样才能自动几秒钟后关闭该消息框?

I am currently developing an application in C# where I display a MessageBox. How can I automatically close the message box after a couple of seconds?

推荐答案

您需要创建自己的窗口,与代码隐藏包含加载处理程序和一个计时器处理程序如下:

You will need to create your own Window, with the code-behind containing a loaded handler and a timer handler as follows:

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()出现:

You can then make your custom message box appear by calling ShowDialog():

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

这篇关于在C#中自动关闭消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆