Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 10th, 2011, 12:19 AM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: Tennessee
Posts: 46
Thanks: 27
Thanked 0 Times in 0 Posts
Default Ex 18-3 FormClosing question

Is there a way of adding the FormClosing routine automatically (like double clicking a button) instead of copying it from code or the msdn library?
Also, your code uses

Code:
'...Handles MyBase.FormClosing'
while the MSDN library uses

Code:
'...Handles Form1.FormClosing'
The 'MyBase' works where the 'Form1' does not work even though the form is identified as Form1. Why?

Keith Z.
Reply With Quote
  #2 (permalink)  
Old August 10th, 2011, 11:58 AM
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

Quote:
Is there a way of adding the FormClosing routine automatically (like double clicking a button) instead of copying it from code or the msdn library?
Also, your code uses
Yes. Copying the text is a pain, although it you are omitting the parameters that makes it a bit easier. If you're not omitting them, then getting them righ can be tricky.

When you double-click a control, you get its default event handler. For a button that's Click, which is pretty useful. For a form it's Load, which is also useful, but sometimes you want something else like FormClosing.

You have two options:

1. In the Form Designer, click on the form. Then in the Properties window, click the event button (the lightning bolt). Now double-click on the event that you want.

2. In the code editor, use the left dropdown at the top to pick the control (or the form) that you want to give an event handler. Then use the right dropdown to pick the event.

Both methods will make you an empty event handler.

Quote:
The 'MyBase' works where the 'Form1' does not work even though the form is identified as Form1. Why?
Cool! I never noticed that before.

In the Handles clause, the thing in front of the event should be an instance of a class. In this case, it should be an instance of Form1, not the Form1 class itself. MyBase works because that means "the instance from which I (the form) inherit."

Interestingly I just noticed (for the first time) that when you use method 1 above you get MyBase but when you use method 2 above you get Me. That also works because Me means "the instance that is currently executing this code."

I'm not sure why MSDN uses Form1. In some versions of VB, there is a special variable named after the form class that is an instance of the class. In this case, there is a variable named Form1. You can actually display that form without explicitly instantiating it.

It was always a bad idea of Microsoft's to include that variable. It's confusing, causes some weird bugs, and solves a problem that almost no one had.

All I can guess is that this variable no longer exists so the code worked in previous versions but doesn't any more. It's possible that you're looking at articles for older versions.

To summarize, I use one of the two methods above to make non-default event handlers. They both work fine. I wouldn't worry about the difference between MyBase and Me.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
The Following User Says Thank You to Rod Stephens For This Useful Post:
zavodney (August 10th, 2011)
  #3 (permalink)  
Old August 10th, 2011, 12:35 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: Tennessee
Posts: 46
Thanks: 27
Thanked 0 Times in 0 Posts
Default

Thank you. I forgot/hadn't thought of method 1 using the properties window and I didn't know about method 2 in the code editor. Now I have 2 easy methods depending on what I have on the screen at the time. They sure beat trying to find the info at the MSDN website, especially if you are not sure of the name of what you are looking for. Thanks again.

Keith Z.
Reply With Quote
Reply


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
Chapter 18 branding walk through question... dlevine Real World SharePoint 2010: Indispensable Experiences from 23 SharePoint MVPs 1 May 19th, 2011 12:18 AM
Migrating to Linq to Sql (pg. 16/18) question Rachel BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 January 19th, 2009 03:15 PM



All times are GMT -4. The time now is 09:34 PM.


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