Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically close messagebox in C#

Tags:

c#

wpf

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?

like image 427
Boardy Avatar asked Dec 06 '10 00:12

Boardy


1 Answers

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);
}

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

MyWindow w = new MyWindow();
w.ShowDialog();
like image 67
Greg Sansom Avatar answered Sep 18 '22 13:09

Greg Sansom