The names of routines ending in _Click(), _Change(), _<etc>() preceded by the name of a control or other object are usually cast in stone.
You can see which names are reserved to a given object by using the 2 drop-down lists at the top of the
VB code editing window when you are in the module of a form. One of the lists has all of the objects and named 'things' (one of which will be "Form") while the other gets its content changed as you select diferent named 'things' with the 1st drop-down list.
There is a predefined routine for every textbox which routine is named _Change(). If the box is named txtMyMy (for instance), the routine will be named txtMyMy_Change(). (Routines that are associated with things this way are called "events.")
The name of the event is crucial. If you are in the code window of a form with a textbox of that name, and you just start typing "Private Sub txtMyMy_Change()," when you hit the Enter key the "End Sub" will be generated for you, and the drop-down lists will change. One will have "txtMyMy" in it, the other "Change." The association of the event with the object will be automatic, strictly based on name. Rename a control, and all the events you have written for it will become orphaned...
If you copy as you indicated, and the control name is the same, the association
will happen, and it is purely based on the name of the control. (There is not similar action in code modules that are
not part of a form.)
When you get to
VB.NET, the lash-up is more intentional. You can name an event anything you like, but you tack on "Handles . . ." at the end to name which events will turn to that routine for code. In this way one single routine can "handle" the events of more than one object. You could (if you were bored) write a routine that changes the background color of a form to pink for 0.25Secs everytime a control loses focusâany control. You could write one routine to do this, and specify in the Handles suffix each of the controls whose _LostFocus() event will call that routine.
But you're right, the association in
VB is less clear.
The arguments in the events are predefined as well. If there is an argument which
VB creates named "Cancel," setting that to True within the routine will cancel the action. (Of course, this variable will only be created for events that lend themselves to being canceled...) The name of the argument is [u]not</u> cast in stoneâif you really want to you can change it.
Let's take the Cancel argument as an example. The behavior of the controlâthat is, the code that makes up the control but which you do
not have access to: how to handle keystrokes, how to handle highlighting, etc.âwill generate a Boolean before calling the cancelable event, then will pass a reference to that variable, and will finally test its value when the event routine has finished. Let's say it is _BeforeUpdate(). The intrinsic code will [u]behave</u> like this 9
this is pseudo-code):
Code:
Dim cnc As Boolean
This.BeforeUpdate(cnc)
If cnc = True Then
AbortMyself()
End If
The part
you will see wil be like:
Code:
Private Sub TheCtrl_BeforeUpdate(Cancel As Boolean)
Code:
' Do my stuff here
If Me.TheCtrl.Text = "" Then
Cancel = True
End If
End Sub
You could re-write this to
Code:
Private Sub TheCtrl_BeforeUpdate(StopIt As Boolean)
' Do my stuff here
If Me.TheCtrl.Text = "" Then
StopIt = True
End If
End Sub
without any change in behavior. Cancel is a local, routine-scope Boolean, and you can call it what you like; it still will modify the same memory location by reference under any name, and the calling routine will still be able to behave in accordance with that value.