I need to load form 2 and wait for 5 seconds to automatically open form 3 how do I do it?
private void LetTheGameStart_Load(object sender, EventArgs e)
{
timer1.Start();
}
This is the only code I have right now
I need to load form 2 and wait for 5 seconds to automatically open form 3 how do I do it?
private void LetTheGameStart_Load(object sender, EventArgs e)
{
timer1.Start();
}
This is the only code I have right now
In particular, I find it extremely unnecessary to use a timer for this type of operation. Use a timer when you want to repeat something within a certain time, or for cases that require a little more work. You can use Task.Delay()
to make your application wait for a certain amount of time without crashing the GUI.
private async void LetTheGameStart_Load(object sender, EventArgs e)
{
await Task.Delay(5000);
AbrirForm2(); // Coloque aqui o código para abrir o form2
}
With Task.Delay
you get the desired delay. await
does not lock the application.
Timer
If you want to insist on Timer
(this may even be better in older versions of the .NET Framework) you will need to set a Interval
for this timer and a Tick
event. The Interval
is the time (in milliseconds) that this timer will be triggered and Tick
is the event that will be executed. Here's an example:
public partial class Form1 : Form
{
private readonly Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 5000; //Definir o intervalo em 5 segundos
timer.Tick += timer_Tick; // Inscrever o evento tick
}
private void timer_Tick(object sender, EventArgs e)
{
new Form().Show(); // Aqui deve-se abrir o Form2
timer.Stop(); // Parar o timer, porque isso só é necessário uma vez
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Start(); // Iniciar o timer quando o form for carregado
}
}