Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a check before closing form?

Tags:

c#

winforms

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);
        }

    }
}
like image 493
3D-kreativ Avatar asked Dec 07 '25 13:12

3D-kreativ


2 Answers

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();
    }
}
like image 171
Ed Chapel Avatar answered Dec 10 '25 01:12

Ed Chapel


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:

  • Solution 1: Pass in the movieManager object into MovieForm through a parameterized constructor. This way you can check the list before closing the form (on the button's click event).
  • Solution 2: Create a static MovieManager.GetMovieFromList method so that you aren't required to instantiate it.

I hope this makes sense.

like image 34
Dave New Avatar answered Dec 10 '25 02:12

Dave New



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!