2

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

    }
}
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
  • Oops, i misunderstood your question. Sorry about that. If i understood your question properly now, you have 2 forms, one parent which launches a child form. Now on entering some title in the child form your closing it, but you do not want it to happen right? In that case use delegates and events. Hook up an event from parent form to child form. When user enters title in child form, raise an event to parent and check if title already exists. – Zenwalker Jun 06 '12 at 15:18
  • Yes, I dont want to close the child form if the title already exists that I check in the parent form. Delegates and events? – 3D-kreativ Jun 06 '12 at 15:25

4 Answers4

3

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();
    }
}
Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
  • Hi, I get a red line below sender and the e and the error message says: A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else – 3D-kreativ Jun 06 '12 at 16:18
  • You must already be using `e` somewhere else in your method. I'll update the answer using `args` for you. – Ed Chapel Jun 06 '12 at 17:16
  • I'm not sure have to use this code. Should this code be inside the first event that I have in my code: private void btnNewMovie_Click(object sender, EventArgs e) { your code.... } ?? – 3D-kreativ Jun 06 '12 at 17:35
  • I updated the answer again to better reflect how you are using it. – Ed Chapel Jun 06 '12 at 17:54
  • Thanks for all your work, but this isn't working! I get a red line below the 'sender' in movieForm.Closing += (sender, args) => – 3D-kreativ Jun 06 '12 at 17:59
  • The window will not open when I click on the button, btnNewMovie. And there is a '}' missing in the end of your code. Could this be solved? :) – 3D-kreativ Jun 06 '12 at 19:50
  • Now it's working! Great! I'm just a little bit confused over the third last '}' that has to end with a ';' and the line: movieForm.Closing += (s, a) => Is this anonymous methods? And I haven't yet used 'using' whats that for? Is it similar to Try Catch? Thanks for the help! :) – 3D-kreativ Jun 06 '12 at 21:33
  • You're welcome! `movieForm.Closing += (s, a) => { };` is a lambda (essentially the same as an anon method). The `using` is a `try/finally` construct that calls `Dispose` as the variable loses scope. – Ed Chapel Jun 06 '12 at 22:38
1

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.

Dave New
  • 38,496
  • 59
  • 215
  • 394
0

You still have the movieForm object. You could do movieForm.ShowDialog() again. Don't forget to fill in the edit fields again with the values in onShow or similar method.

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

Move the check/add code inside your MovieForm and then you can simply just call movieForm.ShowDialog(). You could also raise an event to the main form that the movie was added.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55