Wrox Programmer Forums
|
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2012, 05:04 PM
Registered User
 
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?
 
Old July 14th, 2012, 01:57 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi cellison,

I think a later chapter deals with the problem of the main form trying to repost a form that has been destroyed.

For modal dialogs, by far the most common strategy is to make the button's Click event handler create a new form every time. That resets the form so you know it is in its starting state and more importantly keeps everything local so you don't have those class-level form instances floating around to confuse people.

If data needs to be saved and used to initialize the dialog before it is displayed, the main form stores it somewhere in variables.

People sometimes use another strategy for forms that are not modal dialogs. For example, suppose you have a bunch of secondary forms that the user may want to display often, possibly at the same time. In that case you can use the approach followed by Lesson 9.

To prevent the form from being destroyed, you can make the FormClosing event handler cancel the close and hide the form. Note that with this approach, you cannot easily "cancel" the form.

I'll post some examples in the next couple days on C# Helper.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 18 - TryIt sendalot BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 June 6th, 2012 10:30 AM
Chapter 11 TryIt Luthios BOOK: Java Programming 24-Hour Trainer by Yakov Fain 8 March 3rd, 2012 01:57 PM
TryIt 32 zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 5 October 13th, 2011 11:33 AM
Lesson 36 Tryit Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 4 October 11th, 2011 02:49 PM
Chapter 10 Tryit Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 20th, 2011 08:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.