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.
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.