Reputation: 17
I'm working with two windows forms app: form 1 and form 2. Form 2 is the first form that appears are run-time and it prompts the user to start Form 1 with a click of a button. The objective is, if the button is not clicked in seconds, this form closes and Form 1 opens.
I thought my code was working optimally but it isn't so when I tested it in several ways. The first test, waiting 5 seconds without pressing the start button, Form 1 opens. However, when I clicked on the start button, Form 1 opens and within 5 seconds, another Form 1 opens. This isn't exactly what I'm expecting. I only want one Form 1 to open.
My code is as shown:
Timer t2 = new Timer(); private bool startButtonWasClicked = false;
public Form2()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
startButtonWasClicked = true;
bool IsOpen = false;
foreach(Form f in Application.OpenForms)
{
if(f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
//if the form is already open, it will focus on that form
}
}
if (IsOpen == false)
{
Form1 f1 = new Form1();
f1.Show();
}
//only one Form1 will be allowed when Start button is clicked.
}
private void Form2_Load(object sender, EventArgs e)
{
//if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
if (startButtonWasClicked)
{
t2.Interval = 5000;
t2.Tick += new EventHandler(OnTimerTicker);
t2.Start();
}
//else start button is clicked
}
private void OnTimerTicker(object sender, EventArgs e)
{
t2.Stop();
Form1 f1 = new Form1();
this.Hide();//"closes form 2 after 5 seconds and opens form 1
f1.Show();
}
I'm not sure if I am missing a vital portion but from the looks of the test results, there's certainly something missing.
Upvotes: 0
Views: 571
Reputation: 36
I think you're saying something different than your code. Assuming you mean what you're saying, then your if-statment here should be the opposite:
private void Form2_Load(object sender, EventArgs e)
{
//if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
// Don't really need this because a user won't be able to click the button before the form loads
if (!startButtonWasClicked)
{
t2.Interval = 5000;
t2.Tick += new EventHandler(OnTimerTicker);
t2.Start();
}
//else start button is clicked
}
The order of WinForms can be seen here, which you can look at if you're interested, but the important take away is that the Load() method is invoked when the first form, Form 2, is opened. That starts off the timer with the handler.
Even if a user then clicks, once they get to the handler ('OnTimerTicker'), you will need a condition to check whether you should open another Form1. Something like:
private Timer t2 = new Timer();
private bool startButtonWasClicked = false;
private void Form2_Load(object sender, EventArgs e)
{
//if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
if (!startButtonWasClicked)
{
t2.Interval = 5000;
t2.Tick += new EventHandler(OnTimerTicker);
t2.Start();
}
//else start button is clicked
}
private void button1_Click(object sender, EventArgs e)
{
startButtonWasClicked = true;
// Stop the timer so it doesn't still run
t2.Stop();
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
//if the form is already open, it will focus on that form
}
}
if (IsOpen == false)
{
Form1 f1 = new Form1();
f1.Show();
}
// Hide this window to be consistent
this.Hide();
}
private void OnTimerTicker(object sender, EventArgs e)
{
if (startButtonWasClicked) { return; }
t2.Stop();
Form1 f1 = new Form1();
this.Hide();//"closes form 2 after 5 seconds and opens form 1
f1.Show();
}
There are other pieces you could improve as well to refactor and also hide/not hide consistently, but I think this addresses your issue.
Upvotes: 1
Reputation: 593
You may need to stop the timer when button is clicked in btnStart_Click
.
What I can see is timer is started anyways when Form2 is loaded in Form2_Load
. So, in case button is clicked then timer needs to be stopped, otherwise it will triggered as per set interval.
Upvotes: 0