Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2012, 05:04 PM
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?
  #2 (permalink)  
Old July 14th, 2012, 01:57 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
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!)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.