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

Last reviewed: June 21, 1995
Article ID: Q73808
The information in this article applies to:
  • Microsoft Visual Basic programming system for Windows, versions 1.0, 2.0, and 3.0

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


Additional reference words: 1.00 2.00 3.00
KBCategory: kbenv kbprg kbprb
KBSubcategory: EnvtDes


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.