Disposing a form

  • Thread starter Thread starter Davis
  • Start date Start date
D

Davis

Hi, I have a main form which loads another form. I was wondering how to
dispose of the second form. I have some code below is this correct? Does the
dispose method have to be called after the form has exited.

Private Sub button_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles form2.Click

Dim frm As New frmScan

frm.Show()

frm.Dispose()

End Sub
 
Sorry not quite what i meant. I have a Main form which calls another form
(Form1) which then calls another form Form2. Form2 when it closes returns
to Form1

Private Sub Done_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Done.Click

Me.Close() 'Closes Form2

End Sub



The problem i have is i am not sure that Form2 is disposed let alone Form1. I have a big memory leak

How do i handle this scenario to prevent memory leaks. Where do i need to call Dispose method for Form 2

and Form 1.



Thanks
 
Dispose Form2 in Form1 using the Dispose() method on return to Form1 from Form2. If you implement your own Dispose method this is where you should clean up any unmanaged, and SQL connections etc.

Regards
Simon.
Sorry not quite what i meant. I have a Main form which calls another form
(Form1) which then calls another form Form2. Form2 when it closes returns
to Form1

Private Sub Done_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Done.Click

Me.Close() 'Closes Form2

End Sub



The problem i have is i am not sure that Form2 is disposed let alone Form1. I have a big memory leak

How do i handle this scenario to prevent memory leaks. Where do i need to call Dispose method for Form 2

and Form 1.



Thanks
 
Thanks Simon, would i do it like this

frm = New Form1

frm.ShowDialog()

frm.Dispose

Dispose Form2 in Form1 using the Dispose() method on return to Form1 from Form2. If you implement your own Dispose method this is where you should clean up any unmanaged, and SQL connections etc.

Regards
Simon.
Sorry not quite what i meant. I have a Main form which calls another form
(Form1) which then calls another form Form2. Form2 when it closes returns
to Form1

Private Sub Done_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Done.Click

Me.Close() 'Closes Form2

End Sub



The problem i have is i am not sure that Form2 is disposed let alone Form1. I have a big memory leak

How do i handle this scenario to prevent memory leaks. Where do i need to call Dispose method for Form 2

and Form 1.



Thanks
 
Hi Davis,

Yes this code is correct.

Regards
Simon.
Thanks Simon, would i do it like this

frm = New Form1

frm.ShowDialog()

frm.Dispose

Dispose Form2 in Form1 using the Dispose() method on return to Form1 from Form2. If you implement your own Dispose method this is where you should clean up any unmanaged, and SQL connections etc.

Regards
Simon.
Sorry not quite what i meant. I have a Main form which calls another form
(Form1) which then calls another form Form2. Form2 when it closes returns
to Form1

Private Sub Done_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Done.Click

Me.Close() 'Closes Form2

End Sub



The problem i have is i am not sure that Form2 is disposed let alone Form1. I have a big memory leak

How do i handle this scenario to prevent memory leaks. Where do i need to call Dispose method for Form 2

and Form 1.



Thanks
 
Sorry just one more question if i displayed a form with

frm.Show i can't put the frm.Dispose straight after. Where would
this go in this case?
Hi Davis,

Yes this code is correct.

Regards
Simon.
Thanks Simon, would i do it like this

frm = New Form1

frm.ShowDialog()

frm.Dispose

Dispose Form2 in Form1 using the Dispose() method on return to Form1 from Form2. If you implement your own Dispose method this is where you should clean up any unmanaged, and SQL connections etc.

Regards
Simon.
Sorry not quite what i meant. I have a Main form which calls another form
(Form1) which then calls another form Form2. Form2 when it closes returns
to Form1

Private Sub Done_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Done.Click

Me.Close() 'Closes Form2

End Sub



The problem i have is i am not sure that Form2 is disposed let alone Form1. I have a big memory leak

How do i handle this scenario to prevent memory leaks. Where do i need to call Dispose method for Form 2

and Form 1.



Thanks
 
Back
Top