Deleting VB Control Moves Associated Code to Object: (General)

ID Number: Q73808

1.00

WINDOWS

Summary:

When you delete a control in the Visual Basic environment, the code

that you wrote for that control is not deleted, but is instead moved

to the Object: (General) area for code. Some programmers may not

realize that these detached (unused) procedures still exist and

consume memory. The detached code is available for calling, copying,

or reuse. For example, when you add a control that shares the same

name as a detached procedure, the detached event procedure

reassociates with that control.

If you want to delete both the control and its associated code, you

need to manually select and delete the code in each event procedure

for that control in addition to deleting the control itself.

This behavior is by design in Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

The following example demonstrates that your code goes into the

Object: (General) area after you delete the associated control (or

object):

1. From the File menu, choose New Project.

2. Double-click on a command button from the Toolbox. This puts a

Command1 button on your form.

3. Double-click on the Command1 button. This brings up the code window

for the Command1_Click event procedure.

4. Enter the following code inside the Command1_Click procedure:

Sub Command1_Click()

Print "Hello" 'Enter this statement

End Sub

5. Press F5 to run your code. Click on the Command1 button to see the

text "Hello" display on Form1 in the upper left corner. From

the Run menu, choose End to stop the program.

6. Click on the Command1 button on Form1 to set the focus there and

either press DELETE or choose the Delete command from the Edit menu.

This deletes the Command1 button from the form.

At this point, some programmers may incorrectly assume that the

code associated with the Command1 button was also deleted.

Actually, the code associated with any deleted object is

automatically moved into the Object: (general) area of the Code

window for that form. You can find the detached procedures in the

Code window by choosing (general) from the Object: box, and

choosing the procedures from the Proc: box. (Click on the DOWN

arrow symbol on the right of the Object: and Proc: boxes to see

your choices.)

Additional reference words: 1.00