INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Close Forms Used In Current Form

Close Forms Used In Current Form

(OP)
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

RE: Close Forms Used In Current Form

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

If FindRecordForm IsNot Nothing Then 
    FindRecordForm.Dispose()
    FindRecordForm = Nothing
EndIf 

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!

RE: Close Forms Used In Current Form

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close