what is Me and Form_Unload

emia
08-26-2009, 10:28 AM
Would you mind to tell me what is "Me" and Form_Unload

"Me"

I try Debug.Print Me 'no any return

Me.hWnd 'return some number
Me.hDC ...

And

I known Form_Load(), but Form_Unload, when need use it ?


thank you!

Private Sub Form_Unload(Cancel As Integer)

Mayainc
08-26-2009, 11:20 AM
Me is just a variable for the current form/app that is active,
you could use "Unload Me" for example to close your application
putting it on a double click event perhapse, when you double click your form,
your app will close down just like if you pressed an exit button

Try this on a form and see the magic.
Private Sub Form_DblClick()
Unload Me
End Sub

Gruff
08-28-2009, 01:07 AM
Mayainc is not quite correct.

'Me' is a substitute for the current Form. It cannot be used with .bas Modules.

Any Property or Method you can access from the current form you can access with Me.

Say you have a form named 'frmMasterDataCollection'
If the following code resides insed the form frmMasterDataCollection.

Me.BackColor = vbBlue
is the same as:
frmMasterDataCollection.BackColor = vbBlue

Unload Me will only terminate the program if it is used within the intial Form.
but be aware that it does not check to make sure any other open program forms have been properly closed down.

In a project with more than one form 'Unload Me' used insde a form called from the initial form will only unload the current form not close down the application.

If your initial object is a bas modue (Using a Sub Main() ) and the first form is shown from that sub routine. Unload Me within that form will close that form and the program control will resume back inside your Sub Main() sub routine.

Illusionist
08-28-2009, 01:46 AM
to follow up Form_Unload is called when the form is unloaded.

kassyopeia
08-28-2009, 02:39 AM
Say you have a form named 'frmMasterDataCollection'
If the following code resides insed the form frmMasterDataCollection.

Me.BackColor = vbBlue
is the same as:
frmMasterDataCollection.BackColor = vbBlue
Gruff, you forgot to mention that this description only applies when one is using VB's default form instances. In general, it's a little different:

code module:

Public mdc As frmMasterDataCollection

Public Sub Main
Set mdc = New frmMasterDataCollection
End Sub


frmMasterDataCollection:

frmMasterDataCollection.BackColor = vbBlue 'refers to the default instance
mdc.BackColor = vbBlue 'refers to the instance created in Sub Main
Me.BackColor = vbBlue 'refers to THIS instance


If this seems confusing, think about the difference between "Adam is hungry", "Bob is hungry", and "I am hungry". The information content of the last statement depends on who says it.

Gruff
08-29-2009, 01:38 PM
Considering the level of the original question. I would think delving deeper than VB6 default forms a bit too much. Truth be told I thought perhaps my post was overkill. :-\

kassyopeia
08-29-2009, 01:47 PM
Truth be told I thought perhaps my post was overkill. :-\
Perhaps, but it never hurts to give more information than asked for, right? :)

emia
08-30-2009, 06:54 AM
Thanks, this code.

(1) Always NOT use ME, changed Form1 ? easy to read?

(2) Unload()ALL Form(s). If I need only Unload(Form1). Wrong ?

(3) Is safe to control, when Always keep use Form_Unload(),
and What is (Cancel As Integer) ?

many thanks!


Private Sub Form_Unload(Cancel As Integer)

Dim frm As Form
For Each frm In Forms
MsgBox "unload "
Unload frm
Next

End Sub





Private Sub Form_Load()

Form1.Visible = True
Form2.Visible = True
Form3.Visible = True


MsgBox "form load form1"
Unload Form1

End Sub



Private Sub Form_Unload(Cancel As Integer)

Dim frm As Form
For Each frm In Forms
MsgBox "unload "
Unload frm
Next

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum