I have a Form1 that has a button on it. When the button is clicked Form2 will open up.
The new form that pops up asks the user to enter in data to save to a file and when the Form2‘s button is clicked it saves the entered user data to a specified path.
Here is the Form2‘s button click code:
private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
if (firstNameTextBox.Text.Equals(string.Empty) || lastNameTextBox.Text.Equals(string.Empty) || payTextBox.Text.Equals(string.Empty))
{
MessageBox.Show("Please fill out all of the fields.", "Fields Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//This is where I am saving the data to a file using StreamWrriter.
this.Close();
}
So now that Form2 has closed a new Form2 will open up, etc, etc.. (There is a loop in Form1 that does this).
My problem is…
In Form2 I also have a _FormClosed event that looks like this:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult userAnswer = MessageBox.Show ("Do you wish to close ALL " + counterLabel.Text + " of the 'Form2' forms?", "Close ALL Forms?",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (userAnswer == DialogResult.Yes)
this.Dispose();
}
So my question is…
How do I call the button click to properly close the Form2 WITHOUT calling the _FormClosed event while keeping the functionality of the forms the same?
EDIT:
I know this.Hide(); will do the trick.. but I am wondering if there is a way to not hide all of the Form2‘s since there could be a large amount..
You can’t avoid FormClosed being called but you can add some logic to ignore it.
e.g. set a flag when you are force closing the form (something like
bool _forceClose) and check for that flag in yourFormClosedevent. If it is set, then just return and ignore the rest of the statements.