The code below is in MainFrame.cs and it opens and checks the MovieForm.cs. I want to check if a entered movie title already exist, before a new movie is added to a list. But the problem is, if the title already exist and the messagebox appears, then the MovieForm.cs is already closed and all other data gone and there is no possibilities for the user to change the title to another one! Could this be done in some other way that isn't to complicated? Is there a way to stop the closing of the form? Thanks!
private void btnNewMovie_Click(object sender, EventArgs e)
{
movieForm = new MovieForm();
if (movieForm.ShowDialog() == DialogResult.OK)
{
if (!movieManager.GetMovieFromList(index).Split(',') [0].Equals(movieForm.GetTitle))
{
movieManager.AddNewMovieToMediaLibrary(movieForm.GetNewMovie); // Anropar properties i objektet movieManager
UppdateListboxOfMovies();
}
else
{
MessageBox.Show("Det finns redan en film med titeln " + movieManager.GetMovieFromList(index).Split(',')[0], "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
You have an opportunity to cancel the form closing:
private void btnNewMovie_Click(object sender, EventArgs e)
{
using (var movieForm = new MovieForm())
{
movieForm.Closing += (s, a) =>
{
if (movieForm.DialogResult == DialogResult.OK)
{
if (!movieManager.GetMovieFromList(index).Split(',') [0].Equals(movieForm.GetTitle))
{
movieManager.AddNewMovieToMediaLibrary(movieForm.GetNewMovie); // Anropar properties i objektet movieManager
UppdateListboxOfMovies();
}
else
{
MessageBox.Show("Det finns redan en film med titeln " + movieManager.GetMovieFromList(index).Split(',')[0], "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// Prevent the form from closing and let the user try again
a.Cancel = true;
}
}
};
movieForm.ShowDialog();
}
}
The movieForm object is still in scope, so you can still access any public data from it. I assume that movieForm.GetTitle is returning correctly. All you need to do now is apply the following correction because at the moment you are just comparing your title with the first title in the list:
if (!movieManager.GetMovieFromList(index).Split(',').Contains(movieForm.GetTitle))
...
That should solve your problem.
Edit: Ok, I misunderstood your problem. You want the form to stay open so that the user can make corrections. Possible solutions:
I hope this makes sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With