Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Forms Used In Current Form

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
On many of my forms I open and display other forms with ShowDialog(). At the top of the form I have.
Code:
Private FindRecordForm As RecordFindForm
Private DisplayDetailForm  As DisplayDetailForm
Somewhere in the code of the form I wiil have something like this
Code:
 If FindRecordForm Is Nothing Then
      FindRecordForm = New RecordFindForm
      FindRecordForm.PropSearchType = FormTableName
    End If
    If FindRecordForm.ShowDialog() = DialogResult.OK Then
      SelectData(FindRecordForm.PropSelectedPK)
    End If
These forms are then closed in the "calling" form closing event as follows.
Code:
' Sample Form Cleanup
If Not FindRecordForm Is Nothing Then
  FindRecordForm.Close()
  FindRecordForm.Dispose()
  FindRecordForm = Nothing
End If
If Not DisplayDetailForm Is Nothing Then
  DisplayDetailForm.Close()
  DisplayDetailForm.Dispose()
  DisplayDetailForm= Nothing
End If
Question # 1 - Do I have to do this?.
Question # 2 - If I'm doing this is there a way to find all of them and close them without repeating the code?


Auguy
Sylvania/Toledo Ohio
 
You shouldn't need to close the dialogs in the calling form's Close event handler...the dialog forms are already closed. They have to be, or code execution would still be paused on the ShowDialog line. If you are worried about memory usage, you can Dispose the dialog form right after it's used:

Code:
If FindRecordForm Is Nothing Then
      FindRecordForm = New RecordFindForm
      FindRecordForm.PropSearchType = FormTableName
    End If
    If FindRecordForm.ShowDialog() = DialogResult.OK Then
      SelectData(FindRecordForm.PropSelectedPK)
    End If

[red]If FindRecordForm IsNot Nothing Then 
    FindRecordForm.Dispose()
    FindRecordForm = Nothing
EndIf[/red]

This also keeps the code for opening, closing and disposing each form in the same place.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks. Some of these forms have information on them that the user wants or needs if they display the form more than once. An example is one of the forms is a search form where I maintain any parameters they entered. These will still be there if they display the form again. I don't want to dispose this form until they leave the calling form.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top