PRB: Deleting VB Control Doesn't DeIete Its Code from Memory (73808)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0

This article was previously published under Q73808

SYMPTOMS

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.

RESOLUTION

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.

STATUS

This behavior is by design.

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 a command button from the Toolbox to put a Command1 button on your form.
  3. Double-click the Command1 button to bring 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"
       End Sub
    						
  5. Press the F5 key to run the code. Click 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 the Command1 button on Form1 to set the focus there and either press the DELETE key 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 the DOWN arrow symbol on the right of the Object: and Proc: boxes to see your choices.)

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:kbprb KB73808