Wrox Programmer Forums
|
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 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 August 10th, 2011, 12:19 AM
Authorized User
 
Join Date: May 2011
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.
 
Old August 10th, 2011, 11:58 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
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!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
zavodney (August 10th, 2011)
 
Old August 10th, 2011, 12:35 PM
Authorized User
 
Join Date: May 2011
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.





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





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