Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a form I want to show it for a period of time (say 10 seconds) and then close it,
Can I use a code in the Form_Load function to display the form for this time before closing it.

Please help me I need a code for doing this.

Thanks in advance,
:)
Posted
Updated 23-Nov-18 0:04am

This code closes the form after 10s:
C#
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void Form1_Load(object sender, EventArgs e)
{
  timer.Interval = 10000;
  timer.Tick += new EventHandler(timer_Tick);
  timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
  this.Close();
}
 
Share this answer
 
Comments
Espen Harlinn 17-Mar-11 10:17am    
Nice answer, my 5
Аslam Iqbal 17-Mar-11 11:14am    
my 5 too
charles henington 17-Mar-11 19:37pm    
yet another 5
fjdiewornncalwe 17-Mar-11 21:10pm    
+5. Can't be any clearer than that.
Yes.
C#
Timer formClose = new Timer();
private void myForm_Load(object sender, EventArgs e)
    {
    formClose.Interval = 10000;
    formClose.Tick += new EventHandler(formClose_Tick);
    formClose.Start();
    }

void formClose_Tick(object sender, EventArgs e)
    {
    formClose.Stop();
    formClose.Tick -= new EventHandler(formClose_Tick);
    this.Close();
    }
 
Share this answer
 
Comments
charles henington 17-Mar-11 19:37pm    
My vote of 5.
Also.

From MainForm

mySecondForm frmSecondForm = new mySecondForm();
frmSecondForm.ShowDialog();


From Second Form

C#
Timer formCloser = new Timer();
private void mySecondForm_Load(object sender, EventArgs e)
{
    formCloser.Interval = 10000;
    formCloser.Enabled = true;
    frmCloser.Tick += new EventHandler(formClose_Tick)
}

private void formClose_Tick(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
}
 
Share this answer
 
The best way is to use Thread.Sleep(10000)
and then apply the code this.close
this will wait for 10 s and afterwards form will be closed
 
Share this answer
 
Comments
CHill60 23-Nov-18 8:14am    
Using Thread.Sleep will ... er put the thread to sleep and is absolutely the wrong way to approach this (seven year old!) problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900