Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

destroy Form

Posted on 2003-03-20
7
Medium Priority
?
400 Views
Last Modified: 2011-10-03
I'm pretty new with VB and need some help from Experts. How can I correctly destroy a Form that has been created at runtime with "new" operator? Here's an example...

maincode.bas module contains:
Sub Main()
    Dim oForm As frmFirst
    Set oForm = New frmFirst
    oForm.Show 1
    Set oForm = Nothing
End Sub

frmFirst has a button:
private sub butExit_onClick()
    Me.Hide
end sub

When I closed application with a cross at top-right, everything seems to be ok. When I just press "butExit" button, some memory is still held by an application and I must click "stop" in VB IDE or kill process with task manager. WHat am I doing wrong please?

Thanks in advance,
Martin

0
Comment
Question by:gilipolla
7 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8178087
unload oForm
Set oForm = Nothing

0
 

Expert Comment

by:MrDark
ID: 8178164
Any Form You load using the .show method when u want to destroy it you have to use unload to clear the memory held by it so the hide method just clear it from the screen and u can reshow the form again using the .show method and u also will notice that if this form has a text file and the user wrote anything on it when u reshow it it will appear with the the string entered be the user on the textfield and u can apply that on the optionbox and check box also. So I agree with johnMcCann
0
 
LVL 11

Expert Comment

by:supunr
ID: 8178171
when you exit use the option END.  this will clean any open windows and end the program.  Put the End option anywhere in the program.  But when you want to close a form use "Unload Me" option rather than hiding the form.


Eg.
Unload Me ' unlad this form
End ' end Program

Good Luck!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Accepted Solution

by:
JohnMcCann earned 200 total points
ID: 8178188
Just one last thought

Sub Main()
   Dim oForm As frmFirst
   Set oForm = New frmFirst
   Load oForm
   oForm.Show vbModal
   UnLoad oForm
   Set oForm = Nothing
End Sub

0
 
LVL 1

Expert Comment

by:ph66
ID: 8178687
Put at top of form:

Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long


In code:
                      I.E: destroywindow(form2.hwnd)



replace form2 with the name of the form
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8178918
supnur---NEVER EVER use the End statement to terminate a program.  That is a VERY VERY bad habit.


It is like smashing a car into a brick wall to stop it, rather than applying the brakes.  Yes, END stops the program (RIGHT THEN AND THERE, with NO cleanup), but then so does smahing the car into the wall stop the car...but I would not want to be riding in the car when that happens.

If the program does not stop when all of the forms re unloaded and set to nothing, then there is soemthng else that needs to be cleaned up, and that MUST be addressed, to PRPOERLY terminate the program.  END is a VERY bad way to do it.

AW
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8179656
End is bad - unless you have to stop an activeX.exe with circular references to another activeX.exe (the only circumstance where Microsoft suggests you  to use End statement). But this is very rare...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question