Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close DialogBox programmatically

Tags:

c#

I create a simple form that pops up a dialog box when you run it, but i cannot close it programmatically. Can anyone help me with that?

Label lb = new Label();
Form frm = new Form();
lb.Left = 100;
lb.Top = 44;
frm.Controls.Add(lb);
frm.ShowDialog();
System.Threading.Thread.Sleep(2000);

After ..Sleep(2000) i want it to close. I tried:

 frm.close();frm.dispose();frm.visible = false;

Thanks in advance!

like image 233
Mark Roll Avatar asked Sep 08 '16 06:09

Mark Roll


1 Answers

You can use something like this inside you form class:

protected override async void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    await Task.Delay(2000);
    Close();
}

Inside the OnLoad method run a task that wait 2000 ms and close the form.

like image 126
Alessandro D'Andria Avatar answered Sep 22 '22 10:09

Alessandro D'Andria