"End" or "Me.Close()" ?

Try to avoid END as it doesn't always close resources down correctly - as a rule if you have to use End to get your app to exit then you are failing to close your own resources down.
 
Last edited:
Are you sure mutant? I ran me.close on a subform while i had checkboxes checked, i pulled the form back up and the checkboxes were still checked (it remembered their state)... calling me.dispose forgot the state and the next time i pulled the form up from within my app the checkboxes werent checked. im trying to find out the best way to close a subform. dispose seemed to take longer than me.close.
 
I bet the garbage collector will eventually reclaim anything you have sitting around after a me.close(). Yes, me.dispose() will probably whipe out the memory when you want it done, but it is not required when closing your form.
 
robplatt said:
ran me.close on a subform while i had checkboxes checked, i pulled the form back up and the checkboxes were still checked (it remembered their state)... calling me.dispose forgot the state and the next time i pulled the form up from within my app the checkboxes werent checked.
Not positive that I understand you correctly, but Me.Close should dispose the form. After calling either of the two it won't be in a usable state, i.e. you can't pull it back up and check the state of controls. I think you may have been re-creating the form without realizing it...

Try the following code on a form that has a single button named button1:
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
    Me.Show()
End Sub
You will get an exception complaining about the use of a disposed object.
 
PlausiblyDamp said:
If you use ildasm on the system.windows.forms.dll and look at the form class then you can see Close doesn't call dispose.
No, not directly, but you need to dig a little deeper. Calling the Form.Close method causes the form to send a close message (WM_CLOSE, 0x10), which is in turn handled in WndProc, which sends the message to the WmClose function, where you can find the following [decompiled] code
C#:
// ...
if ((m.Msg != 0x11) && !args1.Cancel) {
    if (this.IsMdiContainer) {
        foreach (Form form2 in this.MdiChildren) {
            if (form2.IsHandleCreated) {
                form2.OnClosed(EventArgs.Empty);
            }
        }
    }
    this.OnClosed(EventArgs.Empty);
    base.Dispose();
}
Note the non-virtual call to Component.Dispose, which will in turn make a virtual call to the Dispose(bool) method, causing the form to be disposed.
 
Ok. I have my startup form. i have a button1 click that does form2.showdialog(), form2 pops up i have some check boxes, i check a couple, then i click close which calls me.close(). i wait a few seconds, then click my button on my startup form which calls form2.showdialog() again. those checkboxes i checked are still checked.

I did the SAME test with me.dispose() instead and it took longer to close, but when i went back in the checkboxes were all cleared. it looked ugly as it repainted my startup form where the top form had covered it. so i went back to me.close, which closes really quick, it just doesnt forget the form controls when i go back in.

so, i manually cleared the state of each checkbox before i closed, is there a better command say on form_load that will refresh all the controls without having to go in and do each one? your gonna say loop through them. no. i'd rather drop the form out of memory completly.
 
Rob,

Here's the way you should do it:

When you click your button to show the new form, you should have a routine that does something like the following (button click sentax is obviously incorrect, but I'm too lazy to look it up right now):
C#:
void Button_Click(whatever parameters)
{
// declare a new instance of the form:
Form2^ objForm = new Form2();
objForm->ShowModal();
// execution will return here after the Modal form has closed
// so collect whatever info you need.
}
 
VB Code
Code:
        Dim frmDBClear = New frmDBClear()
        frmDBClear.ShowDialog()

declaring it as a NEW form did start it as a new form. whudathunkit. thanks joe!
 
A suggestion:

Just so there isn't confusion in your code: You should name the new instance of form something different than the project's actual form. Otherwise, you could get confused as to which form your code is accessing.

For Example:
Visual Basic:
Dim frmDBClear1 = New frmDBClear() 
frmDBClear1.CheckBox1.Checked = True
frmDBClear1.ShowDialog()
Still not clear? Using your code, when you say something like
Visual Basic:
frmDBClear.CheckBox1.Checked = true
Ask yourself, "What is being checked? Is it the new instance that I just created or my project's resource form?" If you don't know, you should certainly be sure to declare your new instance with a different name than you used for your project's resource (a form, in this case). Does that make sense?

Glad to be able to help on here every once in a while. Marble Eatter and PlausiblyDamp have gotten me out of plenty of jams, so it's nice to be able to contribute every once in a while.
 
so you can have multiple instances of your same form huh...

since im showing a dialog and only once at a time, I'll stick with the new one. just so you understand what im doing, the user clicks Help -> database -> clear... I pop up that dialog form, they check off which tables they want to drop, and click ok. those tables are dropped, the form closes. rinse and repeat.

there would be no reason for my program to get confused from multiple instances, however i have learned something from this for future reference.
 
I'm sorry. I haven't used VB8. You must be using "magic forms" (I thing the actual name of the feature is "default instances"), i.e. VB6-style forms. VB optionally allows you to access a form by it's class name instead of explicitly instantiating it and setting it to a variable. When you close or dispose a form, VB injects its own behavior for the form instead of using normal .Net behavior, including the spawning of new forms when an old instance is no longer usable (disposed, for example).

Hence, when you code with your magic forms in VB you will experience different behavior than I do with normal OOP forms in C# (don't start an OOP argument, I'm just referring to different referencing tecniques).
 
I have no problem calling my forms the normal way, its just that the one i need to dispose isnt disposing. i guess... the user may go into that form multiple times while the program is loaded, each time the form gets called it needs to be fresh. joes method of referenceing the form as a new form does that.
 
Back
Top