Thread: Lesson 9 Tryit
View Single Post
  #1 (permalink)  
Old July 13th, 2012, 05:04 PM
cellison cellison is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 9 Tryit

In the TryIt for Lesson 9, you create other forms from the main form. Because you take the instantiation of the forms outside of the button click event (and place it in the main form's loading function), you run into a situation where if one of the windows is closed, and then the button is invoked again, an exception is thrown because you are attempting to do a "Show()" on an instance that no longer exists. It will do this on any of the buttons for which a window is opened and then subsequently closed.

I know there are number of ways to handle this, such as creating a new instance on each button click (bring the instantiation back into the event itself)... Of course, this has the sometimes undesirable effect of enabling the user to generate a bunch of windows and only be able to actively take information from the last one. You could also check for the existence of the form, I guess.

What is the accepted best practice to do this in C# when I want a single form for each button, especially if you have one such as your data entry form in the database chapters, and you have to ensure that things are saved during the "closing" event?