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 February 20th, 2013, 05:44 PM
Registered User
 
Join Date: Feb 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Lesson 21 Try It (Handling Errors)

Hello,

For the most part I understand what the code is saying/doing, however I ran into an odd issue with this lessons example and hoping somebody could explain whats wrong and possibly how to fix it.

When I input new item values correctly and calculate, the OK button enables.
I change a value to something that would have an error and press OK.
An error handler processes and a window appears "Price < 0". I press OK.
The new items form closes when it shouldn't and the list adds the item, with the previous correct calculated values.

Notes:
I changed the OK button to NOT be the accept button
this.DialogResult = DialogResult.OK only occurs if ValuesAreOk() returns true
I can even comment out //this.DialogResult = DialogResult.OK, the same results occur.
---This tells me somewhere in the code/compiler it still believes the Ok button is the default Accept Button...

The only way to fix this error I have found was to erase the Ok button code, delete the button, create a new Okay button, paste the code into its click_event handler... Why do I have to completely delete the button to fix this issue? Is there something silly I am missing or not doing? There isn't much code and objects, I've double checked everything and compared it to the downloaded working example (which works)...

Thanks for the help
 
Old February 20th, 2013, 06:09 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

If the code looks like the downloaded version and it's behaving differently, then there's probably a property that's set differently on the form at design time.

In this case, my guess is that the OK button's DialogResult property is set to OK. (When you make a button the AcceptButton, it sets the DialogResult value and it doesn't change it back if you later make some other button the AcceptButton.)

So I'd check the OK button's DialogResult property and make sure it's set to None. If that doesn't work, let me know.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old February 20th, 2013, 08:27 PM
Registered User
 
Join Date: Feb 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks I found the problem.

I changed the AcceptButton to None on the form itself, however it was still thinking the OK button was the AcceptButton. I looked into the auto generated code and the button still had this code:
this.okayButton.DialogResult = System.Windows.Forms.DialogResult.OK;

Commented it out and the program worked properly...

Is there any reason why this auto generated code didnt correct itself when I changed the forms properties? I even saved and rebuilt the form. But at any rate, its good to know where to look for future reference if a similar error persists.
 
Old February 21st, 2013, 10:49 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

These are two different properties.

The form's AcceptButton property determines what button gets fired when the user presses Enter.

A button's DialogResult property determines what value the form returns as a dialog if the button is pressed.

The only relation is that normally when you set the AcceptButton property the IDE may automatically also set the button's DialogResult property. If you unset AcceptButton, it doesn't unset DialogResult.

Instead of commenting out that line of code, you could have reset the button's DialogResult property in the Properties window.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
Mech_1000 (February 27th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 21 Confused econophil BOOK: JavaScript 24-Hour Trainer 1 January 7th, 2013 11:42 AM
Errors LESSON CS 28 & 29 FINAL MRGLENN BOOK: PHP and MySQL 24-Hour Trainer 2 December 7th, 2012 09:06 PM
Lesson 21 Try It Cristina BOOK: Java Programming 24-Hour Trainer by Yakov Fain 0 March 28th, 2012 02:58 AM
handling all types of errors kliu9 Excel VBA 2 March 9th, 2005 01:06 PM





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